using LibraryAPI.Models;
using System.ComponentModel.DataAnnotations;

namespace LibraryAPI.DTOs
{
    // ── BorrowRecord DTOs ────────────────────────────────────────────────────

    public class BorrowRecordDto
    {
        public int Id { get; set; }
        public string BorrowerName { get; set; } = string.Empty;
        public string BorrowerEmail { get; set; } = string.Empty;
        public DateTime BorrowDate { get; set; }
        public DateTime DueDate { get; set; }
        public DateTime? ReturnDate { get; set; }
        public string Status { get; set; } = string.Empty;
        public string? Notes { get; set; }

        // Book info (flat)
        public int BookId { get; set; }
        public string BookTitle { get; set; } = string.Empty;
        public string BookISBN { get; set; } = string.Empty;
        public string AuthorFullName { get; set; } = string.Empty;

        public bool IsOverdue => Status == "Active" && DueDate < DateTime.UtcNow;
        public int DaysOverdue => IsOverdue ? (int)(DateTime.UtcNow - DueDate).TotalDays : 0;
    }

    public class BorrowRecordCreateDto
    {
        [Required]
        [MaxLength(150)]
        public string BorrowerName { get; set; } = string.Empty;

        [Required]
        [EmailAddress]
        [MaxLength(150)]
        public string BorrowerEmail { get; set; } = string.Empty;

        public DateTime BorrowDate { get; set; } = DateTime.UtcNow;

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

        [MaxLength(500)]
        public string? Notes { get; set; }

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

    public class BorrowRecordUpdateDto
    {
        [Required]
        [MaxLength(150)]
        public string BorrowerName { get; set; } = string.Empty;

        [Required]
        [EmailAddress]
        [MaxLength(150)]
        public string BorrowerEmail { get; set; } = string.Empty;

        public DateTime DueDate { get; set; }

        public DateTime? ReturnDate { get; set; }

        [Required]
        public BorrowStatus Status { get; set; }

        [MaxLength(500)]
        public string? Notes { get; set; }
    }

    // ── BorrowRecord filter query params ─────────────────────────────────────

    public class BorrowRecordFilterParams
    {
        public string? BorrowerName { get; set; }
        public string? BorrowerEmail { get; set; }
        public int? BookId { get; set; }
        public BorrowStatus? Status { get; set; }
        public DateTime? BorrowDateFrom { get; set; }
        public DateTime? BorrowDateTo { get; set; }
        public bool? IsOverdue { get; set; }
    }

    // ── Complex: Overdue Report ───────────────────────────────────────────────

    public class OverdueReportItemDto
    {
        public int BorrowRecordId { get; set; }
        public string BorrowerName { get; set; } = string.Empty;
        public string BorrowerEmail { get; set; } = string.Empty;
        public int BookId { get; set; }
        public string BookTitle { get; set; } = string.Empty;
        public string AuthorFullName { get; set; } = string.Empty;
        public string? Genre { get; set; }
        public DateTime BorrowDate { get; set; }
        public DateTime DueDate { get; set; }
        public int DaysOverdue { get; set; }
    }
}
