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

namespace Azaion.Flights.Controllers;

[ApiController]
[Route("flights")]
[Authorize(Policy = "FL")]
public class FlightsController(FlightService flightService, WaypointService waypointService) : ControllerBase
{
    [HttpPost]
    public async Task<IActionResult> Create([FromBody] CreateFlightRequest request)
    {
        var flight = await flightService.CreateFlight(request);
        return Created($"/flights/{flight.Id}", flight);
    }

    [HttpPut("{id:guid}")]
    public async Task<IActionResult> Update(Guid id, [FromBody] UpdateFlightRequest request)
    {
        var flight = await flightService.UpdateFlight(id, request);
        return Ok(flight);
    }

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

    [HttpGet]
    public async Task<IActionResult> GetAll([FromQuery] GetFlightsQuery query)
    {
        var result = await flightService.GetFlights(query);
        return Ok(result);
    }

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

    [HttpPost("{id:guid}/waypoints")]
    public async Task<IActionResult> CreateWaypoint(Guid id, [FromBody] CreateWaypointRequest request)
    {
        var waypoint = await waypointService.CreateWaypoint(id, request);
        return Created($"/flights/{id}/waypoints/{waypoint.Id}", waypoint);
    }

    [HttpPut("{id:guid}/waypoints/{waypointId:guid}")]
    public async Task<IActionResult> UpdateWaypoint(Guid id, Guid waypointId, [FromBody] UpdateWaypointRequest request)
    {
        var waypoint = await waypointService.UpdateWaypoint(id, waypointId, request);
        return Ok(waypoint);
    }

    [HttpDelete("{id:guid}/waypoints/{waypointId:guid}")]
    public async Task<IActionResult> DeleteWaypoint(Guid id, Guid waypointId)
    {
        await waypointService.DeleteWaypoint(id, waypointId);
        return NoContent();
    }

    [HttpGet("{id:guid}/waypoints")]
    public async Task<IActionResult> GetWaypoints(Guid id)
    {
        var waypoints = await waypointService.GetWaypoints(id);
        return Ok(waypoints);
    }
}
