using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PaklenaBasta.Api.Models;
using PaklenaBasta.Api.Repositories;

namespace PaklenaBasta.Api.Controllers;

[ApiController]
[Route("api")]
public class FilmsController : ControllerBase
{
    [HttpGet("filmovi")]
    [Authorize]
    public IActionResult GetFilms()
    {
        return Ok(InMemoryStore.Films);
    }

    [HttpGet("stream/{id}")]
    [Authorize]
    public IActionResult GetStream(string id)
    {
        var film = InMemoryStore.Films.FirstOrDefault(f => f.Id == id);
        if (film == null) return NotFound();
        return Ok(new { id = film.Id, url = film.HlsUrl });
    }

    [HttpPost("filmovi")]
    [Authorize]
    public IActionResult AddFilm([FromBody] Film film)
    {
        if (string.IsNullOrWhiteSpace(film.Title) || string.IsNullOrWhiteSpace(film.HlsUrl))
            return BadRequest(new { message = "Title and HlsUrl are required" });

        InMemoryStore.AddFilm(film);
        return CreatedAtAction(nameof(GetStream), new { id = film.Id }, film);
    }
}
