using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Azaion.Flights.DTOs;
using Azaion.Flights.Services;

namespace Azaion.Flights.Controllers;

[ApiController]
[Route("aircrafts")]
[Authorize(Policy = "FL")]
public class AircraftsController(AircraftService aircraftService) : ControllerBase
{
    [HttpPost]
    public async Task<IActionResult> Create([FromBody] CreateAircraftRequest request)
    {
        var aircraft = await aircraftService.CreateAircraft(request);
        return Created($"/aircrafts/{aircraft.Id}", aircraft);
    }

    [HttpPut("{id:guid}")]
    public async Task<IActionResult> Update(Guid id, [FromBody] UpdateAircraftRequest request)
    {
        var aircraft = await aircraftService.UpdateAircraft(id, request);
        return Ok(aircraft);
    }

    [HttpDelete("{id:guid}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        await aircraftService.DeleteAircraft(id);
        return NoContent();
    }

    [HttpGet]
    public async Task<IActionResult> GetAll([FromQuery] GetAircraftsQuery query)
    {
        var aircrafts = await aircraftService.GetAircrafts(query);
        return Ok(aircrafts);
    }

    [HttpGet("{id:guid}")]
    public async Task<IActionResult> Get(Guid id)
    {
        var aircraft = await aircraftService.GetAircraft(id);
        return Ok(aircraft);
    }

    [HttpPatch("{id:guid}/default")]
    public async Task<IActionResult> SetDefault(Guid id, [FromBody] SetDefaultRequest request)
    {
        await aircraftService.SetDefault(id, request);
        return NoContent();
    }
}
