using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace PSTW_CentralSystem.Areas.Bookings.Models
{
    public enum BookingStatus
    {
        Pending = 0,
        Approved = 1,
        Rejected = 2,
        Cancelled = 3
    }

    [Table("bookings")]
    public class Booking : IValidatableObject
    {
        [Key]
        [Column("BookingId")]
        public int BookingId { get; set; }

        /// <summary>FK → aspnetusers(Id) (int)</summary>
        [Required]
        public int RequestedByUserId { get; set; }

        /// <summary>If booking on behalf of someone else; else null.</summary>
        public int? TargetUserId { get; set; }

        /// <summary>Snapshot of org at submission time.</summary>
        public int? DepartmentId { get; set; }   // FK → departments(DepartmentId)
        public int? CompanyId { get; set; }      // FK → companies(CompanyId)

        /// <summary>Room being booked.</summary>
        [Required]
        public int RoomId { get; set; }          // FK → rooms(RoomId)

        [Required, StringLength(150)]
        public string Title { get; set; } = string.Empty;

        [StringLength(300)]
        public string? Purpose { get; set; }

        /// <summary>Use UTC to avoid TZ headaches; map to DATETIME in MySQL.</summary>
        [Required]
        public DateTime StartUtc { get; set; }

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

        [StringLength(500)]
        public string? Note { get; set; }

        [Required]
        public DateTime CreatedUtc { get; set; } = DateTime.UtcNow;

        [Required]
        public DateTime LastUpdatedUtc { get; set; } = DateTime.UtcNow;

        [Required]
        public BookingStatus CurrentStatus { get; set; } = BookingStatus.Pending;

        // ---- validation ----
        public IEnumerable<ValidationResult> Validate(ValidationContext _)
        {
            if (EndUtc <= StartUtc)
                yield return new ValidationResult("End time must be after start time.",
                    new[] { nameof(EndUtc) });

            if ((EndUtc - StartUtc).TotalMinutes < 10)
                yield return new ValidationResult("Minimum booking duration is 10 minutes.",
                    new[] { nameof(StartUtc), nameof(EndUtc) });
        }
    }

    [Table("rooms")]
    public class Room
    {
        [Key] public int RoomId { get; set; }

        [Required, StringLength(120)]
        public string RoomName { get; set; } = string.Empty;

        [StringLength(40)]
        public string? LocationCode { get; set; }

        public int? Capacity { get; set; }
        public bool IsActive { get; set; } = true;
    }
}
