using System.Security.Claims;
using LearningMaterialSystem.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace LearningMaterialSystem.Controllers;

[Authorize(Roles = "GiangVien")]
public class GiangVienController : Controller
{
    private readonly ApplicationDbContext _context;

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

    private int? CurrentUserId => int.TryParse(User.FindFirstValue(ClaimTypes.NameIdentifier), out var id) ? id : null;

    /// <summary>Trang tổng quan giảng viên: thống kê nhanh học liệu đã đăng.</summary>
    public async Task<IActionResult> Index()
    {
        if (!CurrentUserId.HasValue)
            return RedirectToAction("Index", "Home");

        var tong = await _context.HocLieus.CountAsync(h => h.NguoiDungId == CurrentUserId);
        var choDuyet = await _context.HocLieus.CountAsync(h => h.NguoiDungId == CurrentUserId && h.TrangThai == "ChoDuyet");
        var daDuyet = await _context.HocLieus.CountAsync(h => h.NguoiDungId == CurrentUserId && h.TrangThai == "DaDuyet");
        var tuChoi = await _context.HocLieus.CountAsync(h => h.NguoiDungId == CurrentUserId && h.TrangThai == "TuChoi");

        ViewBag.TongHocLieu = tong;
        ViewBag.ChoDuyet = choDuyet;
        ViewBag.DaDuyet = daDuyet;
        ViewBag.TuChoi = tuChoi;
        return View();
    }
}
