import Foundation
import SwiftUI
import ComposeApp

// MARK: - Document Form State

/// Form state container for creating/editing a document
struct DocumentFormState: FormState {
    var title = FormField<String>()
    var description = FormField<String>()
    var category = FormField<String>()
    var tags = FormField<String>()
    var notes = FormField<String>()
    var selectedDocumentType: String?
    var selectedResidenceId: Int32?
    var selectedContractorId: Int32?
    var isActive: Bool = true

    // For warranties/appliances
    var itemName = FormField<String>()
    var modelNumber = FormField<String>()
    var serialNumber = FormField<String>()
    var provider = FormField<String>()
    var providerContact = FormField<String>()
    var claimPhone = FormField<String>()
    var claimEmail = FormField<String>()
    var claimWebsite = FormField<String>()

    // Dates
    var purchaseDate: Date?
    var startDate: Date?
    var endDate: Date?

    // Images
    var selectedImages: [UIImage] = []

    // For edit mode
    var existingDocumentId: Int32?

    var isEditMode: Bool {
        existingDocumentId != nil
    }

    var isValid: Bool {
        !title.isEmpty &&
        selectedDocumentType != nil &&
        (isEditMode || selectedResidenceId != nil)
    }

    mutating func validateAll() {
        title.validate { ValidationRules.validateRequired($0, fieldName: "Title") }

        // Validate email if provided
        if !claimEmail.isEmpty {
            claimEmail.validate { value in
                ValidationRules.isValidEmail(value) ? nil : .invalidEmail
            }
        }
    }

    mutating func reset() {
        title = FormField<String>()
        description = FormField<String>()
        category = FormField<String>()
        tags = FormField<String>()
        notes = FormField<String>()
        selectedDocumentType = nil
        selectedResidenceId = nil
        selectedContractorId = nil
        isActive = true
        itemName = FormField<String>()
        modelNumber = FormField<String>()
        serialNumber = FormField<String>()
        provider = FormField<String>()
        providerContact = FormField<String>()
        claimPhone = FormField<String>()
        claimEmail = FormField<String>()
        claimWebsite = FormField<String>()
        purchaseDate = nil
        startDate = nil
        endDate = nil
        selectedImages = []
        existingDocumentId = nil
    }

    // MARK: - Date Formatting

    private static let dateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        formatter.locale = Locale(identifier: "en_US_POSIX")
        return formatter
    }()

    var purchaseDateString: String? {
        purchaseDate.map { Self.dateFormatter.string(from: $0) }
    }

    var startDateString: String? {
        startDate.map { Self.dateFormatter.string(from: $0) }
    }

    var endDateString: String? {
        endDate.map { Self.dateFormatter.string(from: $0) }
    }
}
