using ColaFlow.Modules.Identity.Application.Commands.RegisterTenant;
using ColaFlow.Modules.Identity.Application.Queries.GetTenantBySlug;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace ColaFlow.API.Controllers;

[ApiController]
[Route("api/[controller]")]
public class TenantsController(IMediator mediator) : ControllerBase
{
    /// <summary>
    /// Register a new tenant (company signup)
    /// </summary>
    [HttpPost("register")]
    public async Task<IActionResult> Register([FromBody] RegisterTenantCommand command)
    {
        var result = await mediator.Send(command);
        return Ok(result);
    }

    /// <summary>
    /// Get tenant by slug (for login page tenant resolution)
    /// </summary>
    [HttpGet("{slug}")]
    public async Task<IActionResult> GetBySlug(string slug)
    {
        var query = new GetTenantBySlugQuery(slug);
        var result = await mediator.Send(query);

        if (result == null)
            return NotFound(new { message = "Tenant not found" });

        return Ok(result);
    }

    /// <summary>
    /// Check if tenant slug is available
    /// </summary>
    [HttpGet("check-slug/{slug}")]
    public async Task<IActionResult> CheckSlug(string slug)
    {
        var query = new GetTenantBySlugQuery(slug);
        var result = await mediator.Send(query);

        return Ok(new { available = result == null });
    }
}
