using LibraryAPI.Models;
using Microsoft.EntityFrameworkCore;

namespace LibraryAPI.Data
{
    public class LibraryDbContext : DbContext
    {
        public LibraryDbContext(DbContextOptions<LibraryDbContext> options) : base(options) { }

        public DbSet<Author> Authors { get; set; }
        public DbSet<Book> Books { get; set; }
        public DbSet<BorrowRecord> BorrowRecords { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // Author configuration
            modelBuilder.Entity<Author>(entity =>
            {
                entity.HasKey(a => a.Id);
                entity.Property(a => a.FirstName).IsRequired().HasMaxLength(100);
                entity.Property(a => a.LastName).IsRequired().HasMaxLength(100);
                entity.Property(a => a.Biography).HasMaxLength(500);
                entity.Property(a => a.Nationality).HasMaxLength(100);
            });

            // Book configuration
            modelBuilder.Entity<Book>(entity =>
            {
                entity.HasKey(b => b.Id);
                entity.Property(b => b.Title).IsRequired().HasMaxLength(200);
                entity.Property(b => b.ISBN).IsRequired().HasMaxLength(13);
                entity.HasIndex(b => b.ISBN).IsUnique();
                entity.Property(b => b.Genre).HasMaxLength(100);
                entity.Property(b => b.Price).HasColumnType("decimal(10,2)");
                entity.Property(b => b.Description).HasMaxLength(1000);

                entity.HasOne(b => b.Author)
                      .WithMany(a => a.Books)
                      .HasForeignKey(b => b.AuthorId)
                      .OnDelete(DeleteBehavior.Restrict);
            });

            // BorrowRecord configuration
            modelBuilder.Entity<BorrowRecord>(entity =>
            {
                entity.HasKey(br => br.Id);
                entity.Property(br => br.BorrowerName).IsRequired().HasMaxLength(150);
                entity.Property(br => br.BorrowerEmail).IsRequired().HasMaxLength(150);
                entity.Property(br => br.Notes).HasMaxLength(500);
                entity.Property(br => br.Status)
                      .HasConversion<string>()
                      .HasMaxLength(20);

                entity.HasOne(br => br.Book)
                      .WithMany(b => b.BorrowRecords)
                      .HasForeignKey(br => br.BookId)
                      .OnDelete(DeleteBehavior.Restrict);
            });

            // Seed data
            modelBuilder.Entity<Author>().HasData(
                new Author { Id = 1, FirstName = "George", LastName = "Orwell", Nationality = "British", Biography = "English novelist and essayist, journalist and critic.", CreatedAt = new DateTime(2024, 1, 1) },
                new Author { Id = 2, FirstName = "Jane", LastName = "Austen", Nationality = "British", Biography = "English novelist known primarily for her six major novels.", CreatedAt = new DateTime(2024, 1, 1) },
                new Author { Id = 3, FirstName = "Frank", LastName = "Herbert", Nationality = "American", Biography = "American science fiction author best known for Dune.", CreatedAt = new DateTime(2024, 1, 1) }
            );

            modelBuilder.Entity<Book>().HasData(
                new Book { Id = 1, Title = "1984", ISBN = "9780451524935", Genre = "Dystopian", PublishedYear = 1949, Price = 12.99m, TotalCopies = 5, AvailableCopies = 3, AuthorId = 1, CreatedAt = new DateTime(2024, 1, 1) },
                new Book { Id = 2, Title = "Animal Farm", ISBN = "9780451526342", Genre = "Political Satire", PublishedYear = 1945, Price = 10.99m, TotalCopies = 4, AvailableCopies = 4, AuthorId = 1, CreatedAt = new DateTime(2024, 1, 1) },
                new Book { Id = 3, Title = "Pride and Prejudice", ISBN = "9780141439518", Genre = "Romance", PublishedYear = 1813, Price = 9.99m, TotalCopies = 6, AvailableCopies = 5, AuthorId = 2, CreatedAt = new DateTime(2024, 1, 1) },
                new Book { Id = 4, Title = "Sense and Sensibility", ISBN = "9780141439662", Genre = "Romance", PublishedYear = 1811, Price = 9.99m, TotalCopies = 3, AvailableCopies = 3, AuthorId = 2, CreatedAt = new DateTime(2024, 1, 1) },
                new Book { Id = 5, Title = "Dune", ISBN = "9780441013593", Genre = "Science Fiction", PublishedYear = 1965, Price = 14.99m, TotalCopies = 7, AvailableCopies = 6, AuthorId = 3, CreatedAt = new DateTime(2024, 1, 1) }
            );
        }
    }
}
