//
//  PaymentCollectionRecordViewController.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 收款记录页，展示近 7 天按日分组的收款明细。
final class PaymentCollectionRecordViewController: BaseViewController {

    private let viewModel = PaymentCollectionRecordViewModel()
    private let paymentAPI = NetworkServices.shared.paymentAPI

    private let scrollView = UIScrollView()
    private let contentStack = UIStackView()
    private let emptyLabel = UILabel()
    private let recordCardView = UIView()
    private let groupsStack = UIStackView()
    private let footerLabel = UILabel()

    override func setupNavigationBar() {
        title = "收款记录"
    }

    override func setupUI() {
        view.backgroundColor = AppColor.pageBackground

        contentStack.axis = .vertical
        contentStack.spacing = AppSpacing.md

        emptyLabel.text = "暂无收款记录"
        emptyLabel.font = .systemFont(ofSize: 14)
        emptyLabel.textColor = AppColor.textTertiary
        emptyLabel.textAlignment = .center
        emptyLabel.isHidden = true

        recordCardView.backgroundColor = .white
        recordCardView.layer.cornerRadius = AppRadius.lg
        recordCardView.isHidden = true

        groupsStack.axis = .vertical
        groupsStack.spacing = AppSpacing.md

        footerLabel.text = "仅支持查看7天的数据"
        footerLabel.font = .systemFont(ofSize: 12)
        footerLabel.textColor = UIColor(hex: 0xB6BECA)
        footerLabel.textAlignment = .center
        footerLabel.isHidden = true

        view.addSubview(scrollView)
        scrollView.addSubview(contentStack)
        contentStack.addArrangedSubview(emptyLabel)
        contentStack.addArrangedSubview(recordCardView)
        contentStack.addArrangedSubview(footerLabel)
        recordCardView.addSubview(groupsStack)
    }

    override func setupConstraints() {
        scrollView.snp.makeConstraints { make in
            make.edges.equalTo(view.safeAreaLayoutGuide)
        }
        contentStack.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(AppSpacing.screenHorizontalInset)
            make.width.equalTo(scrollView.snp.width).offset(-AppSpacing.screenHorizontalInset * 2)
        }
        emptyLabel.snp.makeConstraints { make in
            make.height.greaterThanOrEqualTo(240)
        }
        groupsStack.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(AppSpacing.md)
        }
    }

    override func bindActions() {
        viewModel.onStateChange = { [weak self] in
            Task { @MainActor in self?.applyViewModel() }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        Task { await loadData() }
    }

    private func loadData() async {
        showLoading()
        await viewModel.loadRecord(api: paymentAPI)
        hideLoading()
    }

    @MainActor
    private func applyViewModel() {
        let hasData = !viewModel.groups.isEmpty
        emptyLabel.isHidden = hasData
        recordCardView.isHidden = !hasData
        footerLabel.isHidden = !hasData

        groupsStack.arrangedSubviews.forEach {
            groupsStack.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }

        viewModel.groups.forEach { group in
            let groupView = PaymentRecordGroupView()
            groupView.apply(group: group)
            groupsStack.addArrangedSubview(groupView)
        }
    }
}
