using comissions.app.api.Extensions;
using comissions.app.database;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace comissions.app.api.Controllers;

[ApiController]
[Authorize("admin")]
[Route("api/admin/[controller]")]
public class AdminArtistsController:ControllerBase
{
    private readonly ApplicationDbContext _dbContext;

    public AdminArtistsController(ApplicationDbContext dbContext)
    {
        _dbContext = dbContext;
    }
    
    [HttpGet]
    public async Task<IActionResult> GetArtists(string search="", int offset = 0, int pageSize = 10)
    {
        var sellers = await _dbContext.UserArtists.Include(x=>x.User)
            .Where(x=>x.User.DisplayName.Contains(search) || x.User.Email.Contains(search))
            .Skip(offset).Take(pageSize).ToListAsync();
        return Ok(sellers);
    }
    
    [HttpGet("Count")]
    public async Task<IActionResult> GetArtistsCount(string search="")
    {
        var result = await _dbContext.UserArtists.Include(x=>x.User)
            .Where(x=>x.User.DisplayName.Contains(search) || x.User.Email.Contains(search))
            .CountAsync();
        return Ok(result);
    }
    
    [HttpGet("{sellerId:int}")]
    public async Task<IActionResult> GetArtist(int sellerId)
    {
        var seller = await _dbContext.UserArtists.Include(x=>x.User)
            .FirstOrDefaultAsync(x=>x.Id==sellerId);

        if (seller == null)
            return NotFound();
        
        return Ok(seller);
    }
    
    
    [HttpPut("{sellerId:int}/Suspend")]
    public async Task<IActionResult> SuspendArtist(int sellerId, [FromQuery]string reason, [FromQuery]int days)
    {
        var seller = _dbContext.UserArtists.FirstOrDefault(x=>x.Id==sellerId);
        
        if (seller == null)
            return NotFound();

        if (seller.Suspended)
            return BadRequest();
        
        seller.Suspended = true;
        seller.SuspendedDate = DateTime.UtcNow;
        seller.UnsuspendDate = DateTime.UtcNow.AddDays(days);
        seller.SuspendedReason = reason;
        seller.SuspendAdminId = User.GetUserId();
        _dbContext.UserArtists.Update(seller);
        
        await _dbContext.SaveChangesAsync();
        return Ok();
    }
    
    [HttpPut("{sellerId:int}/Unsuspend")]
    public async Task<IActionResult> UnsuspendArtist(int sellerId)
    {
        var seller = _dbContext.UserArtists.FirstOrDefault(x=>x.Id==sellerId);
        
        if (seller == null)
            return NotFound();

        if (!seller.Suspended)
            return BadRequest();
        
        seller.Suspended = false;
        seller.SuspendedDate = null;
        seller.UnsuspendDate = null;
        seller.SuspendedReason = null;
        seller.SuspendAdminId = null;
        _dbContext.UserArtists.Update(seller);
        
        await _dbContext.SaveChangesAsync();
        return Ok();
    }
    
    [HttpPut("{sellerId:int}/Terminate")]
    public async Task<IActionResult> TerminateArtist(int sellerId)
    {
        var seller = _dbContext.UserArtists.FirstOrDefault(x=>x.Id==sellerId);
        
        if (seller == null)
            return NotFound();

        if (!seller.Suspended)
            return BadRequest();

        _dbContext.UserArtists.Remove(seller);
        await _dbContext.SaveChangesAsync();
        return Ok();
    }
    
    [HttpPut("{sellerId:int}/SetBiography")]
    public async Task<IActionResult> SetBiography(int sellerId, [FromBody]string biography)
    {
        var seller = _dbContext.UserArtists.FirstOrDefault(x=>x.Id==sellerId);
        
        if (seller == null)
            return NotFound();

        if (!seller.Suspended)
            return BadRequest();

        seller.Description = biography;
        _dbContext.UserArtists.Update(seller);
        await _dbContext.SaveChangesAsync();
        return Ok();
    }
}