using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NoticeBoard.Data;
using NoticeBoard.Models;

namespace NoticeBoard.Controllers;

[Authorize]
public class SlidesController : Controller
{
    private readonly AppDbContext _db;

    public SlidesController(AppDbContext db)
    {
        _db = db;
    }

    public async Task<IActionResult> Index()
    {
        var slides = await _db.Slides
            .Include(s => s.DeviceSlides)
                .ThenInclude(ds => ds.Device)
            .OrderBy(s => s.Name)
            .ToListAsync();
        return View(slides);
    }

    public IActionResult Create()
    {
        return View(new Slide());
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(Slide slide)
    {
        if (!ModelState.IsValid)
            return View(slide);

        slide.CreatedAt = DateTime.UtcNow;
        slide.UpdatedAt = DateTime.UtcNow;
        _db.Slides.Add(slide);
        await _db.SaveChangesAsync();

        TempData["Success"] = $"Slide '{slide.Name}' created.";
        return RedirectToAction(nameof(Index));
    }

    public async Task<IActionResult> Edit(int id)
    {
        var slide = await _db.Slides.FindAsync(id);
        if (slide == null) return NotFound();
        return View(slide);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int id, Slide slide)
    {
        if (id != slide.Id) return NotFound();
        if (!ModelState.IsValid) return View(slide);

        var existing = await _db.Slides.FindAsync(id);
        if (existing == null) return NotFound();

        existing.Name = slide.Name;
        existing.SlideType = slide.SlideType;
        existing.Content = slide.Content;
        existing.EmbedUrl = slide.EmbedUrl;
        existing.IcsSource = slide.IcsSource;
        existing.CustomCss = slide.CustomCss;
        existing.BackgroundColor = slide.BackgroundColor;
        existing.BackgroundImage = slide.BackgroundImage;
        existing.BackgroundSize = slide.BackgroundSize;
        existing.UpdatedAt = DateTime.UtcNow;

        await _db.SaveChangesAsync();
        TempData["Success"] = $"Slide '{slide.Name}' updated.";
        return RedirectToAction(nameof(Index));
    }

    public async Task<IActionResult> Delete(int id)
    {
        var slide = await _db.Slides
            .Include(s => s.DeviceSlides)
                .ThenInclude(ds => ds.Device)
            .FirstOrDefaultAsync(s => s.Id == id);
        if (slide == null) return NotFound();
        return View(slide);
    }

    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> DeleteConfirmed(int id)
    {
        var slide = await _db.Slides.FindAsync(id);
        if (slide == null) return NotFound();

        _db.Slides.Remove(slide);
        await _db.SaveChangesAsync();

        TempData["Success"] = $"Slide '{slide.Name}' deleted.";
        return RedirectToAction(nameof(Index));
    }

    public async Task<IActionResult> Preview(int id)
    {
        var slide = await _db.Slides.FindAsync(id);
        if (slide == null) return NotFound();
        return View(slide);
    }
}
