using MemberCenter.Application.Abstractions;
using MemberCenter.Application.Constants;
using MemberCenter.Web.Authorization;
using MemberCenter.Web.Models.Admin;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace MemberCenter.Web.Areas.Admin.Controllers;

[Area("Admin")]
[Authorize(Policy = "Admin")]
[AdminPermission(AdminPermissions.Blacklist)]
[Route("admin/blacklist")]
public class BlacklistController : Controller
{
    private readonly IEmailBlacklistService _blacklistService;

    public BlacklistController(IEmailBlacklistService blacklistService)
    {
        _blacklistService = blacklistService;
    }

    [HttpGet("")]
    [AdminPermission(AdminPermissions.BlacklistIndex)]
    public async Task<IActionResult> Index()
    {
        var items = await _blacklistService.ListAsync();
        return View(items);
    }

    [HttpGet("create")]
    [AdminPermission(AdminPermissions.BlacklistCreate)]
    public IActionResult Create()
    {
        return View(new EmailBlacklistFormViewModel());
    }

    [HttpPost("create")]
    [AdminPermission(AdminPermissions.BlacklistCreate)]
    public async Task<IActionResult> Create(EmailBlacklistFormViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        await _blacklistService.AddOrUpdateAsync(
            model.Email,
            model.Reason,
            "admin",
            DateTimeOffset.UtcNow);

        return RedirectToAction("Index");
    }
}
