using System.ComponentModel.DataAnnotations;
using PowderCoating.Core.Enums;

namespace PowderCoating.Application.DTOs.Maintenance;

public class MaintenanceRecordDto
{
    public int Id { get; set; }
    public int EquipmentId { get; set; }
    public string EquipmentName { get; set; } = string.Empty;
    public string MaintenanceType { get; set; } = string.Empty;
    public string Status { get; set; } = string.Empty;
    public string StatusDisplay { get; set; } = string.Empty;
    public string Priority { get; set; } = string.Empty;
    public string PriorityDisplay { get; set; } = string.Empty;

    public DateTime ScheduledDate { get; set; }
    public DateTime? CompletedDate { get; set; }
    public string? PerformedById { get; set; }
    public string? PerformedByName { get; set; }
    public string? AssignedUserId { get; set; }
    public string? AssignedWorkerName { get; set; }

    public string Description { get; set; } = string.Empty;
    public string? WorkPerformed { get; set; }
    public string? PartsReplaced { get; set; }

    public decimal LaborCost { get; set; }
    public decimal PartsCost { get; set; }
    public decimal TotalCost { get; set; }

    public decimal DowntimeHours { get; set; }

    public string? Notes { get; set; }
    public string? TechnicianNotes { get; set; }

    // Recurrence
    public bool IsRecurring { get; set; }
    public string? RecurrenceFrequency { get; set; }   // display string, e.g. "Weekly"
    public DateTime? RecurrenceEndDate { get; set; }
    public string? RecurrenceGroupId { get; set; }
    public int? RecurrenceParentId { get; set; }

    public DateTime CreatedAt { get; set; }
    public DateTime? UpdatedAt { get; set; }
}

public class MaintenanceListDto
{
    public int Id { get; set; }
    public int EquipmentId { get; set; }
    public string EquipmentName { get; set; } = string.Empty;
    public string MaintenanceType { get; set; } = string.Empty;
    public string Status { get; set; } = string.Empty;
    public string StatusDisplay { get; set; } = string.Empty;
    public string Priority { get; set; } = string.Empty;
    public string PriorityDisplay { get; set; } = string.Empty;
    public DateTime ScheduledDate { get; set; }
    public DateTime? CompletedDate { get; set; }
    public decimal TotalCost { get; set; }

    // Recurrence
    public bool IsRecurring { get; set; }
    public string? RecurrenceGroupId { get; set; }
}

public class CreateMaintenanceDto
{
    [Required(ErrorMessage = "Equipment is required")]
    [Display(Name = "Equipment")]
    public int EquipmentId { get; set; }

    [Required(ErrorMessage = "Maintenance type is required")]
    [StringLength(100, ErrorMessage = "Maintenance type cannot exceed 100 characters")]
    [Display(Name = "Maintenance Type")]
    public string MaintenanceType { get; set; } = string.Empty;

    [Required(ErrorMessage = "Status is required")]
    [StringLength(50, ErrorMessage = "Status cannot exceed 50 characters")]
    [Display(Name = "Status")]
    public string Status { get; set; } = "Scheduled";

    [Required(ErrorMessage = "Priority is required")]
    [StringLength(50, ErrorMessage = "Priority cannot exceed 50 characters")]
    [Display(Name = "Priority")]
    public string Priority { get; set; } = "Normal";

    [Required(ErrorMessage = "Scheduled date is required")]
    [Display(Name = "Scheduled Date")]
    public DateTime ScheduledDate { get; set; }

    [Display(Name = "Completed Date")]
    public DateTime? CompletedDate { get; set; }

    [Display(Name = "Performed By")]
    public string? PerformedById { get; set; }

    [Required(ErrorMessage = "Description is required")]
    [StringLength(2000, ErrorMessage = "Description cannot exceed 2000 characters")]
    [Display(Name = "Description")]
    public string Description { get; set; } = string.Empty;

    [StringLength(2000, ErrorMessage = "Work performed cannot exceed 2000 characters")]
    [Display(Name = "Work Performed")]
    public string? WorkPerformed { get; set; }

    [StringLength(2000, ErrorMessage = "Parts replaced cannot exceed 2000 characters")]
    [Display(Name = "Parts Replaced")]
    public string? PartsReplaced { get; set; }

    [Range(0, 9999999.99, ErrorMessage = "Labor cost must be between 0 and 9,999,999.99")]
    [Display(Name = "Labor Cost")]
    public decimal LaborCost { get; set; }

    [Range(0, 9999999.99, ErrorMessage = "Parts cost must be between 0 and 9,999,999.99")]
    [Display(Name = "Parts Cost")]
    public decimal PartsCost { get; set; }

    [Range(0, 999999, ErrorMessage = "Downtime hours must be between 0 and 999,999")]
    [Display(Name = "Downtime Hours")]
    public decimal DowntimeHours { get; set; }

    [StringLength(2000, ErrorMessage = "Notes cannot exceed 2000 characters")]
    [Display(Name = "Notes")]
    public string? Notes { get; set; }

    [StringLength(2000, ErrorMessage = "Technician notes cannot exceed 2000 characters")]
    [Display(Name = "Technician Notes")]
    public string? TechnicianNotes { get; set; }

    // Recurrence
    [Display(Name = "Recurring Maintenance")]
    public bool IsRecurring { get; set; }

    [Display(Name = "Frequency")]
    public MaintenanceRecurrenceFrequency? RecurrenceFrequency { get; set; }

    [Display(Name = "Recurrence End Date")]
    public DateTime? RecurrenceEndDate { get; set; }
}

public class UpdateMaintenanceDto : CreateMaintenanceDto
{
    [Required]
    public int Id { get; set; }
}
