using Microsoft.AspNetCore.Mvc;
using LittleShop.DTOs;
using LittleShop.Services;

namespace LittleShop.Controllers;

[ApiController]
[Route("api/[controller]")]
public class DevController : ControllerBase
{
    private readonly IProductService _productService;

    public DevController(IProductService productService)
    {
        _productService = productService;
    }

    [HttpPost("variations")]
    public async Task<ActionResult<ProductVariationDto>> CreateVariationForDev(CreateProductVariationDto createVariationDto)
    {
        try
        {
            var variation = await _productService.CreateProductVariationAsync(createVariationDto);
            return CreatedAtAction("GetProductVariation", "ProductVariations", new { id = variation.Id }, variation);
        }
        catch (ArgumentException ex)
        {
            return BadRequest(ex.Message);
        }
    }

    [HttpGet("products")]
    public async Task<ActionResult> GetProductsWithIds()
    {
        var products = await _productService.GetAllProductsAsync();
        var result = products.Select(p => new {
            id = p.Id,
            name = p.Name,
            price = p.Price,
            variationCount = p.Variations.Count
        });
        return Ok(result);
    }
}