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

namespace foundation_system.Controllers;

[Authorize]
public class CargoColaboradorController : Controller
{
    private readonly ApplicationDbContext _context;

    public CargoColaboradorController(ApplicationDbContext context)
    {
        _context = context;
    }

    // GET: CargoColaborador
    public async Task<IActionResult> Index()
    {
        return View(await _context.CargosColaboradores.OrderBy(c => c.Nombre).ToListAsync());
    }

    // GET: CargoColaborador/Create
    public IActionResult Create()
    {
        return View();
    }

    // POST: CargoColaborador/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("Nombre,Descripcion,Activo")] CargoColaborador cargo)
    {
        if (ModelState.IsValid)
        {
            cargo.CreadoEn = DateTime.UtcNow;
            _context.Add(cargo);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(cargo);
    }

    // GET: CargoColaborador/Edit/5
    public async Task<IActionResult> Edit(long? id)
    {
        if (id == null) return NotFound();

        var cargo = await _context.CargosColaboradores.FindAsync(id);
        if (cargo == null) return NotFound();
        return View(cargo);
    }

    // POST: CargoColaborador/Edit/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(long id, [Bind("Id,Nombre,Descripcion,Activo,CreadoEn")] CargoColaborador cargo)
    {
        if (id != cargo.Id) return NotFound();

        if (ModelState.IsValid)
        {
            try
            {
                _context.Update(cargo);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CargoExists(cargo.Id)) return NotFound();
                else throw;
            }
            return RedirectToAction(nameof(Index));
        }
        return View(cargo);
    }

    // POST: CargoColaborador/Delete/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Delete(long id)
    {
        var cargo = await _context.CargosColaboradores.FindAsync(id);
        if (cargo != null)
        {
            var isUsed = await _context.Colaboradores.AnyAsync(c => c.CargoId == id);
            if (isUsed)
            {
                TempData["ErrorMessage"] = "No se puede eliminar porque hay colaboradores asignados a este cargo.";
                return RedirectToAction(nameof(Index));
            }

            _context.CargosColaboradores.Remove(cargo);
            await _context.SaveChangesAsync();
        }
        return RedirectToAction(nameof(Index));
    }

    private bool CargoExists(long id)
    {
        return _context.CargosColaboradores.Any(e => e.Id == id);
    }
}
