using Apollinare.API.Hubs;
using Apollinare.API.Services;
using Microsoft.AspNetCore.Mvc;

namespace Apollinare.API.Controllers;

[ApiController]
[Route("api/[controller]")]
public class DemoController : ControllerBase
{
    private readonly DemoDataService _demoDataService;
    private readonly DataNotificationService _notifier;

    public DemoController(DemoDataService demoDataService, DataNotificationService notifier)
    {
        _demoDataService = demoDataService;
        _notifier = notifier;
    }

    /// <summary>
    /// Genera dati di test per dimostrazioni
    /// </summary>
    [HttpPost("generate")]
    public async Task<ActionResult<DemoDataResult>> GenerateDemoData()
    {
        try
        {
            var result = await _demoDataService.GenerateDemoData();
            await _notifier.NotifyBulkChange("demo");
            return Ok(result);
        }
        catch (Exception ex)
        {
            return BadRequest(new { error = ex.Message });
        }
    }

    /// <summary>
    /// Pulisce tutti i dati dal database
    /// </summary>
    [HttpPost("clear")]
    public async Task<ActionResult<DemoDataResult>> ClearDemoData()
    {
        try
        {
            var result = await _demoDataService.ClearDemoData();
            await _notifier.NotifyBulkChange("demo");
            return Ok(result);
        }
        catch (Exception ex)
        {
            return BadRequest(new { error = ex.Message });
        }
    }
}
