using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Tiku.Api.Contracts;
using Tiku.Application.Backoffice;
using Tiku.Application.Security;

namespace Tiku.Api.Controllers;

[ApiController]
[Route("api/backoffice")]
public sealed class BackofficeController(
    IBackofficeService backofficeService,
    ICurrentAccessContext currentAccessContext) : ControllerBase
{
    [HttpGet("tenant/ui-bootstrap")]
    [Authorize(Policy = TikuPolicies.TenantBackofficeBootstrap)]
    [ProducesResponseType<BackofficeUiBootstrap>(StatusCodes.Status200OK)]
    public async Task<ActionResult<BackofficeUiBootstrap>> GetTenantUiBootstrap(CancellationToken cancellationToken)
    {
        return Ok(await backofficeService.GetTenantUiBootstrapAsync(
            await currentAccessContext.GetAsync(cancellationToken),
            cancellationToken));
    }

    [HttpGet("tenant/bootstrap")]
    [Authorize(Policy = BackendPermissions.TenantRoleManage)]
    [ProducesResponseType<BackofficeBootstrap>(StatusCodes.Status200OK)]
    public async Task<ActionResult<BackofficeBootstrap>> GetTenantBootstrap(CancellationToken cancellationToken)
    {
        return Ok(await backofficeService.GetTenantBootstrapAsync(await ResolveTenantActorAsync(cancellationToken), cancellationToken));
    }

    [HttpGet("platform/ui-bootstrap")]
    [Authorize(Policy = TikuPolicies.PlatformBackofficeBootstrap)]
    [ProducesResponseType<BackofficeUiBootstrap>(StatusCodes.Status200OK)]
    public async Task<ActionResult<BackofficeUiBootstrap>> GetPlatformUiBootstrap(CancellationToken cancellationToken)
    {
        return Ok(await backofficeService.GetPlatformUiBootstrapAsync(
            await currentAccessContext.GetAsync(cancellationToken),
            cancellationToken));
    }

    [HttpPost("tenant/roles")]
    [Authorize(Policy = BackendPermissions.TenantRoleManage)]
    [ProducesResponseType<BackofficeRoleItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<BackofficeRoleItem>> UpsertTenantRole(
        UpsertBackofficeRoleDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await backofficeService.UpsertTenantRoleAsync(await ResolveTenantActorAsync(cancellationToken), request.ToCommand(), cancellationToken));
    }

    [HttpPut("tenant/roles/{roleId:guid}/bindings")]
    [Authorize(Policy = BackendPermissions.TenantRoleManage)]
    [ProducesResponseType<BackofficeRoleItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<BackofficeRoleItem>> ReplaceTenantRoleBindings(
        Guid roleId,
        ReplaceRoleBindingsDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await backofficeService.ReplaceTenantRoleBindingsAsync(await ResolveTenantActorAsync(cancellationToken), request.ToCommand(roleId), cancellationToken));
    }

    [HttpPut("tenant/users/{userId:guid}/roles")]
    [Authorize(Policy = BackendPermissions.TenantRoleManage)]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    public async Task<IActionResult> ReplaceTenantUserRoles(
        Guid userId,
        ReplaceUserRolesDto request,
        CancellationToken cancellationToken)
    {
        await backofficeService.ReplaceTenantUserRolesAsync(await ResolveTenantActorAsync(cancellationToken), request.ToCommand(userId), cancellationToken);
        return NoContent();
    }

    [HttpGet("platform/bootstrap")]
    [Authorize(Policy = BackendPermissions.PlatformRoleManage)]
    [ProducesResponseType<BackofficeBootstrap>(StatusCodes.Status200OK)]
    public async Task<ActionResult<BackofficeBootstrap>> GetPlatformBootstrap(CancellationToken cancellationToken)
    {
        return Ok(await backofficeService.GetPlatformBootstrapAsync(await ResolvePlatformActorAsync(cancellationToken), cancellationToken));
    }

    [HttpPost("platform/roles")]
    [Authorize(Policy = BackendPermissions.PlatformRoleManage)]
    [ProducesResponseType<BackofficeRoleItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<BackofficeRoleItem>> UpsertPlatformRole(
        UpsertBackofficeRoleDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await backofficeService.UpsertPlatformRoleAsync(await ResolvePlatformActorAsync(cancellationToken), request.ToCommand(), cancellationToken));
    }

    [HttpPut("platform/roles/{roleId:guid}/bindings")]
    [Authorize(Policy = BackendPermissions.PlatformRoleManage)]
    [ProducesResponseType<BackofficeRoleItem>(StatusCodes.Status200OK)]
    public async Task<ActionResult<BackofficeRoleItem>> ReplacePlatformRoleBindings(
        Guid roleId,
        ReplaceRoleBindingsDto request,
        CancellationToken cancellationToken)
    {
        return Ok(await backofficeService.ReplacePlatformRoleBindingsAsync(await ResolvePlatformActorAsync(cancellationToken), request.ToCommand(roleId), cancellationToken));
    }

    [HttpPut("platform/users/{userId:guid}/roles")]
    [Authorize(Policy = BackendPermissions.PlatformRoleManage)]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    public async Task<IActionResult> ReplacePlatformUserRoles(
        Guid userId,
        ReplaceUserRolesDto request,
        CancellationToken cancellationToken)
    {
        await backofficeService.ReplacePlatformUserRolesAsync(await ResolvePlatformActorAsync(cancellationToken), request.ToCommand(userId), cancellationToken);
        return NoContent();
    }

    private async Task<BackofficeActor> ResolveTenantActorAsync(CancellationToken cancellationToken)
    {
        return BackofficeActor.FromTenantAccess(await currentAccessContext.GetAsync(cancellationToken));
    }

    private async Task<BackofficeActor> ResolvePlatformActorAsync(CancellationToken cancellationToken)
    {
        return BackofficeActor.FromPlatformAccess(await currentAccessContext.GetAsync(cancellationToken));
    }
}
