using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Silk_Road_Api.Extensions;
using Silk_Road_Api.Models;
using Silk_Road_Api.Services;
using Silk_Road_Api.ViewModels;

namespace Silk_Road_Api.Controllers;

[ApiController]
[Route("[controller]")]
public class ManufacturerController : ControllerBase
{
    private readonly IManufacturerService _manufacturerService;

    public ManufacturerController(IManufacturerService manufacturerService)
    {
        _manufacturerService = manufacturerService;
    }

    [HttpGet("get-manufacturer", Name = "GetManufacturers")]
    public async Task<IActionResult> GetManufacturers([FromQuery] SearchInput searchInput)
    {
        var manufacturers = await _manufacturerService.GetManufacturers(this, searchInput);
        return Ok(manufacturers);
    }

    [HttpGet("get-manufacturer/{id}", Name = "GetManufacturer")]
    public async Task<IActionResult> GetManufacturer(int id)
    {
        var manufacturer = await _manufacturerService.GetManufacturer(this, id);
        if (manufacturer == null)
        {
            return NotFound();
        }
        return Ok(manufacturer);
    }

    [HttpPost("add-manufacturer", Name = "AddManufacturer")]
    public async Task<IActionResult> AddManufacturer([FromBody] ManufacturerViewModel manufacturer)
    {
        if (await _manufacturerService.AddManufacturer(this, manufacturer))
            return Ok();
        return BadRequest();
    }

    [HttpPut("update-manufacturer", Name = "UpdateManufacturer")]
    public async Task<IActionResult> UpdateManufacturer([FromBody] ManufacturerViewModel manufacturer)
    {
        if (await _manufacturerService.UpdateManufacturer(this, manufacturer))
            return Ok();
        return BadRequest();
    }

    [HttpDelete("delete-manufacturer/{id}", Name = "DeleteManufacturer")]
    public async Task<IActionResult> DeleteManufacturer(int id)
    {
        if (await _manufacturerService.DeleteManufacturer(this, id))
            return Ok();
        return BadRequest();
    }
}
