using LearningMaterialSystem.Data;
using LearningMaterialSystem.Models;
using LearningMaterialSystem.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace LearningMaterialSystem.Controllers
{
    [Authorize(Roles = "BoPhanChuyenMon")]
    public class NguoiDungsController : Controller
    {
        private readonly ApplicationDbContext _context;

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

        // Danh sách user
        public async Task<IActionResult> Index()
        {
            var users = await _context.NguoiDungs.ToListAsync();
            return View(users);
        }

        // Form tạo user
        public IActionResult Create()
        {
            return View();
        }

        // Tạo user
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(NguoiDung model, string password)
        {
            if (!ModelState.IsValid)
                return View(model);

            bool exist = await _context.NguoiDungs
                .AnyAsync(x => x.TenDangNhap == model.TenDangNhap);

            if (exist)
            {
                ModelState.AddModelError("", "Tên đăng nhập đã tồn tại");
                return View(model);
            }

            model.MatKhauHash = PasswordHelper.Hash(password);

            _context.NguoiDungs.Add(model);
            await _context.SaveChangesAsync();

            return RedirectToAction(nameof(Index));
        }

        // Form sửa
        public async Task<IActionResult> Edit(int id)
        {
            var user = await _context.NguoiDungs.FindAsync(id);
            if (user == null) return NotFound();

            return View(user);
        }

        // Lưu sửa
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, NguoiDung model)
        {
            if (id != model.Id) return NotFound();

            if (!ModelState.IsValid)
                return View(model);

            var user = await _context.NguoiDungs.FindAsync(id);
            if (user == null) return NotFound();

            user.HoTen = model.HoTen;
            user.Email = model.Email;
            user.MaSo = model.MaSo;
            user.VaiTro = model.VaiTro;

            await _context.SaveChangesAsync();

            return RedirectToAction(nameof(Index));
        }

        // Form xóa
        public async Task<IActionResult> Delete(int id)
        {
            var user = await _context.NguoiDungs.FindAsync(id);
            if (user == null) return NotFound();

            return View(user);
        }

        // Xác nhận xóa
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var user = await _context.NguoiDungs.FindAsync(id);

            if (user != null)
            {
                _context.NguoiDungs.Remove(user);
                await _context.SaveChangesAsync();
            }

            return RedirectToAction(nameof(Index));
        }
    }
}