using GermanApp.Application.DTOs.Auth;
using GermanApp.Application.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Net;
namespace GermanApp.Presentation.Controllers;
///
/// Controller for bootstrap operations (first admin user creation).
/// This controller should be removed or disabled after the first admin is created.
/// This is part of the Presentation layer.
///
[ApiController]
[Route("api/[controller]")]
public class BootstrapController : ControllerBase
{
private readonly IAuthService _authService;
public BootstrapController(IAuthService authService)
{
_authService = authService;
}
///
/// Creates the first admin user.
/// This endpoint is PUBLIC (no authentication required) but can only be used once.
/// After creating the first admin, this endpoint will return 400 BadRequest.
///
/// Admin user registration data
/// Authentication response with JWT token
[HttpPost("admin")]
[AllowAnonymous]
[ProducesResponseType(typeof(AuthResponse), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(string), (int)HttpStatusCode.BadRequest)]
[ProducesResponseType(typeof(string), (int)HttpStatusCode.Conflict)]
public async Task CreateAdminUser([FromBody] RegisterDto registerDto)
{
try
{
var result = await _authService.CreateAdminUserAsync(registerDto);
return Ok(result);
}
catch (InvalidOperationException ex)
{
// Admin already exists - this is expected after first use
if (ex.Message.Contains("Admin user already exists"))
return Conflict(ex.Message);
return BadRequest(ex.Message);
}
catch (Exception ex)
{
return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
}
}
///
/// Checks if an admin user already exists.
///
/// True if admin exists, false otherwise
[HttpGet("admin-exists")]
[AllowAnonymous]
[ProducesResponseType(typeof(bool), (int)HttpStatusCode.OK)]
public async Task CheckAdminExists()
{
try
{
var exists = await _authService.AdminUserExistsAsync();
return Ok(exists);
}
catch (Exception ex)
{
return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
}
}
}