import SwiftUI

// MARK: - General Date Picker Component
struct DatePickerView: View {
    let title: String
    let isRequired: Bool
    let date: Binding<Date>
    let displayedComponents: DatePickerComponents
    let icon: String?
    
    init(
        title: String,
        isRequired: Bool = false,
        date: Binding<Date>,
        displayedComponents: DatePickerComponents = [.date, .hourAndMinute],
        icon: String? = nil
    ) {
        self.title = title
        self.isRequired = isRequired
        self.date = date
        self.displayedComponents = displayedComponents
        self.icon = icon
    }
    
    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            InputTitleView.required(title, icon: icon)
            
            DatePicker(title, selection: date, displayedComponents: displayedComponents)
                .datePickerStyle(CompactDatePickerStyle())
                .labelsHidden()
                .padding(.horizontal, 12)
                .padding(.vertical, 8)
                .background(
                    RoundedRectangle(cornerRadius: 8)
                        .fill(Color(.systemGray6))
                )
        }
    }
}

// MARK: - Predefined Date Picker Styles
extension DatePickerView {
    static func dateOnly(
        title: String,
        isRequired: Bool = false,
        date: Binding<Date>,
        icon: String? = nil
    ) -> DatePickerView {
        DatePickerView(
            title: title,
            isRequired: isRequired,
            date: date,
            displayedComponents: .date,
            icon: icon
        )
    }
    
    static func timeOnly(
        title: String,
        isRequired: Bool = false,
        date: Binding<Date>,
        icon: String? = nil
    ) -> DatePickerView {
        DatePickerView(
            title: title,
            isRequired: isRequired,
            date: date,
            displayedComponents: .hourAndMinute,
            icon: icon
        )
    }
    
    static func dateAndTime(
        title: String,
        isRequired: Bool = false,
        date: Binding<Date>,
        icon: String? = nil
    ) -> DatePickerView {
        DatePickerView(
            title: title,
            isRequired: isRequired,
            date: date,
            displayedComponents: [.date, .hourAndMinute],
            icon: icon
        )
    }
    
    static func startDate(
        title: String = "start_time".localized,
        isRequired: Bool = true,
        date: Binding<Date>,
        icon: String? = "calendar"
    ) -> DatePickerView {
        DatePickerView(
            title: title,
            isRequired: isRequired,
            date: date,
            displayedComponents: [.date, .hourAndMinute],
            icon: icon
        )
    }
    
    static func endDate(
        title: String = "end_time".localized,
        isRequired: Bool = true,
        date: Binding<Date>,
        icon: String? = "calendar"
    ) -> DatePickerView {
        DatePickerView(
            title: title,
            isRequired: isRequired,
            date: date,
            displayedComponents: [.date, .hourAndMinute],
            icon: icon
        )
    }
}

#Preview {
    VStack(spacing: 16) {
        DatePickerView.dateOnly(
            title: "select_date".localized,
            date: .constant(Date())
        )
        
        DatePickerView.timeOnly(
            title: "select_time".localized,
            date: .constant(Date())
        )
        
        DatePickerView.dateAndTime(
            title: "select_date_and_time".localized,
            date: .constant(Date())
        )
        
        DatePickerView.startDate(date: .constant(Date()))
        DatePickerView.endDate(date: .constant(Date().addingTimeInterval(3600)))
    }
    .padding()
} 