//
//  RealNameAuthAuditViewController.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 实名认证审核状态页，展示审核结果与证件预览。
final class RealNameAuthAuditViewController: BaseViewController {

    private let info: RealNameInfo

    private let scrollView = UIScrollView()
    private let contentStack = UIStackView()
    private let statusBadge = ProfileStatusBadgeView()
    private let rejectLabel = UILabel()
    private let frontImageView = UIImageView()
    private let backImageView = UIImageView()

    init(info: RealNameInfo) {
        self.info = info
        super.init(nibName: nil, bundle: nil)
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setupNavigationBar() {
        title = "认证状态"
    }

    override func setupUI() {
        view.backgroundColor = UIColor(hex: 0xF7FAFF)
        contentStack.axis = .vertical
        contentStack.spacing = 16

        rejectLabel.numberOfLines = 0
        rejectLabel.font = .systemFont(ofSize: 14)
        rejectLabel.textColor = UIColor(hex: 0xEF4444)

        [frontImageView, backImageView].forEach {
            $0.contentMode = .scaleAspectFit
            $0.backgroundColor = UIColor(hex: 0xF8FAFC)
            $0.layer.cornerRadius = 8
            $0.clipsToBounds = true
            $0.snp.makeConstraints { make in make.height.equalTo(160) }
        }

        view.addSubview(scrollView)
        scrollView.addSubview(contentStack)

        contentStack.addArrangedSubview(makeTitle("审核状态"))
        contentStack.addArrangedSubview(statusBadge)
        if let reason = info.rejectReason, !reason.isEmpty, info.auditStatus == 3 {
            rejectLabel.text = "驳回原因：\(reason)"
            contentStack.addArrangedSubview(rejectLabel)
        }
        contentStack.addArrangedSubview(makeInfoRow("姓名", info.realName))
        contentStack.addArrangedSubview(makeInfoRow("身份证号", maskID(info.idCardNo)))
        contentStack.addArrangedSubview(makeTitle("身份证国徽面"))
        contentStack.addArrangedSubview(backImageView)
        contentStack.addArrangedSubview(makeTitle("身份证人像面"))
        contentStack.addArrangedSubview(frontImageView)

        applyStatus()
        frontImageView.loadRemoteImage(urlString: info.frontUrl)
        backImageView.loadRemoteImage(urlString: info.backUrl)
    }

    override func setupConstraints() {
        scrollView.snp.makeConstraints { make in make.edges.equalToSuperview() }
        contentStack.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(16)
            make.width.equalTo(scrollView).offset(-32)
        }
    }

    private func applyStatus() {
        switch info.auditStatus {
        case 2:
            statusBadge.apply(style: .auditApproved, text: "已实名认证")
        case 3:
            statusBadge.apply(style: .auditRejected, text: "审核不通过")
        default:
            statusBadge.apply(style: .auditPending, text: info.auditStatusText ?? "审核中")
        }
    }

    private func makeTitle(_ text: String) -> UILabel {
        let label = UILabel()
        label.text = text
        label.font = .systemFont(ofSize: 15, weight: .semibold)
        return label
    }

    private func makeInfoRow(_ title: String, _ value: String) -> UILabel {
        let label = UILabel()
        label.font = .systemFont(ofSize: 14)
        label.textColor = AppColor.text666
        label.text = "\(title)：\(value)"
        return label
    }

    private func maskID(_ value: String) -> String {
        guard value.count > 8 else { return value }
        let prefix = value.prefix(4)
        let suffix = value.suffix(4)
        return "\(prefix)**********\(suffix)"
    }
}
