using PSTW_CentralSystem.Areas.Inventory.Models;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace PSTW_CentralSystem.Areas.OTcalculate.Models
{
    [Table("otregisters")]
    public class OtRegisterModel
    {
        [Key]
        public int OvertimeId { get; set; }

        [Required]
        public DateTime OtDate { get; set; }

        public TimeSpan? OfficeFrom { get; set; }
        public TimeSpan? OfficeTo { get; set; }
        public int? OfficeBreak { get; set; }
        public TimeSpan? AfterFrom { get; set; }
        public TimeSpan? AfterTo { get; set; }
        public int? AfterBreak { get; set; }
        public int? StationId { get; set; }

        [ForeignKey("StationId")]
        public virtual StationModel? Stations { get; set; }

        public string OtDescription { get; set; }
        public string OtDays { get; set; }

        [Required]
        public int UserId { get; set; }

        public int? StatusId { get; set; }

        [ForeignKey("StatusId")]
        public virtual OtStatusModel? Otstatus { get; set; }

        // Convert string times to TimeSpan before saving
        public TimeSpan? GetOfficeFrom() => ParseTimeSpan(OfficeFrom?.ToString());
        public TimeSpan? GetOfficeTo() => ParseTimeSpan(OfficeTo?.ToString());
        public TimeSpan? GetAfterFrom() => ParseTimeSpan(AfterFrom?.ToString());
        public TimeSpan? GetAfterTo() => ParseTimeSpan(AfterTo?.ToString());

        private TimeSpan? ParseTimeSpan(string? time)
        {
            return TimeSpan.TryParse(time, out TimeSpan result) ? result : null;
        }

        public class OtRegisterEditDto
        {
            public int OvertimeId { get; set; }
            public DateTime OtDate { get; set; }
            public string? OfficeFrom { get; set; } 
            public string? OfficeTo { get; set; }  
            public int? OfficeBreak { get; set; }
            public string? AfterFrom { get; set; }  
            public string? AfterTo { get; set; }   
            public int? AfterBreak { get; set; }
            public int? StationId { get; set; }
            public string? OtDescription { get; set; }

            public int StatusId { get; set; }
            public string? OtDays { get; set; }
        }

        public class OtUpdateLog
        {
            public string ApproverRole { get; set; } 
            public int ApproverUserId { get; set; }
            public DateTime UpdateTimestamp { get; set; }
            public string ChangeType { get; set; } 


            public OtRegisterModel? BeforeEdit { get; set; }
            public OtRegisterEditDto? AfterEdit { get; set; }

            public OtRegisterModel? DeletedRecord { get; set; }
        }

        public class OtRegisterUpdateDto
        {
            public int OvertimeId { get; set; }
            public DateTime OtDate { get; set; }
            public string? OfficeFrom { get; set; }
            public string? OfficeTo { get; set; }
            public int? OfficeBreak { get; set; }
            public string? AfterFrom { get; set; }
            public string? AfterTo { get; set; }
            public int? AfterBreak { get; set; }
            public int? StationId { get; set; }
            public string? OtDescription { get; set; }
            public string? OtDays { get; set; }
            public int UserId { get; set; }
        }
    }
}
