using Microsoft.EntityFrameworkCore;
using NoticeBoard.Models;

namespace NoticeBoard.Data;

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

    public DbSet<Slide> Slides => Set<Slide>();
    public DbSet<Device> Devices => Set<Device>();
    public DbSet<DeviceSlide> DeviceSlides => Set<DeviceSlide>();

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

        modelBuilder.Entity<Device>()
            .HasIndex(d => d.Slug)
            .IsUnique();

        modelBuilder.Entity<DeviceSlide>()
            .HasOne(ds => ds.Device)
            .WithMany(d => d.DeviceSlides)
            .HasForeignKey(ds => ds.DeviceId)
            .OnDelete(DeleteBehavior.Cascade);

        modelBuilder.Entity<DeviceSlide>()
            .HasOne(ds => ds.Slide)
            .WithMany(s => s.DeviceSlides)
            .HasForeignKey(ds => ds.SlideId)
            .OnDelete(DeleteBehavior.Cascade);

        modelBuilder.Entity<DeviceSlide>()
            .HasIndex(ds => new { ds.DeviceId, ds.DisplayOrder });

        // Seed demo data
        modelBuilder.Entity<Device>().HasData(
            new Device { Id = 1, Name = "Front of House", Slug = "frontofhouse", ResolutionWidth = 1920, ResolutionHeight = 1080 },
            new Device { Id = 2, Name = "Staff Room", Slug = "staffroom", ResolutionWidth = 1920, ResolutionHeight = 1080 }
        );

        modelBuilder.Entity<Slide>().HasData(
            new Slide
            {
                Id = 1,
                Name = "Welcome Screen",
                SlideType = SlideType.Content,
                Content = "<div style=\"display:flex;align-items:center;justify-content:center;height:100%;text-align:center;\"><div><h1 style=\"font-size:4em;margin-bottom:0.3em;\">Welcome</h1><p style=\"font-size:1.8em;opacity:0.8;\">Notice Board</p></div></div>",
                BackgroundColor = "#1a1a2e"
            },
            new Slide
            {
                Id = 2,
                Name = "Welcome to Country",
                SlideType = SlideType.Content,
                Content = "<div style=\"display:flex;align-items:center;justify-content:center;height:100%;text-align:center;padding:2em;\"><div><h1 style=\"font-size:2.5em;margin-bottom:0.5em;\">Acknowledgement of Country</h1><p style=\"font-size:1.3em;line-height:1.8;max-width:800px;\">We acknowledge the Traditional Owners of the land on which we meet and pay our respects to Elders past, present and emerging.</p></div></div>",
                BackgroundColor = "#2d1b00"
            }
        );

        modelBuilder.Entity<DeviceSlide>().HasData(
            new DeviceSlide { Id = 1, DeviceId = 1, SlideId = 1, DisplayOrder = 1, DurationSeconds = 30, Enabled = true },
            new DeviceSlide { Id = 2, DeviceId = 1, SlideId = 2, DisplayOrder = 2, DurationSeconds = 60, Enabled = true },
            new DeviceSlide { Id = 3, DeviceId = 2, SlideId = 1, DisplayOrder = 1, DurationSeconds = 30, Enabled = true },
            new DeviceSlide { Id = 4, DeviceId = 2, SlideId = 2, DisplayOrder = 2, DurationSeconds = 60, Enabled = true }
        );
    }
}
