using Hc900Crawler.Core.Application.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace Hc900Crawler.Web.Controllers;

[ApiController]
[Route("api/experion/hypertable")]
public class HypertableController : ControllerBase
{
    private readonly IExperionDbService _db;

    public HypertableController(IExperionDbService db) => _db = db;

    [HttpGet("status")]
    public async Task<IActionResult> Status()
    {
        var info = await _db.GetHypertableStatusAsync();
        return Ok(new
        {
            info.IsHypertable,
            info.TableName,
            statusMessage = info.StatusMessage,
            info.RecordCount,
            info.HasRetentionPolicy,
            info.HasCompression,
            info.HasContinuousAggregate
        });
    }

    [HttpPost("create")]
    public async Task<IActionResult> Create([FromBody] HypertableCreateRequestDto dto)
    {
        var request = new Hc900Crawler.Infrastructure.Database.HypertableCreateRequest
        {
            TableName = dto.TableName,
            TimeColumn = dto.TimeColumn,
            TimeInterval = dto.TimeInterval,
            MigrateData = dto.MigrateData,
            SetRetentionPolicy = dto.SetRetentionPolicy,
            RetentionPeriod = dto.RetentionPeriod,
            EnableCompression = dto.EnableCompression,
            CompressionPeriod = dto.CompressionPeriod,
            CreateContinuousAggregate = dto.CreateContinuousAggregate
        };
        var result = await _db.CreateHypertableAsync(request);
        return Ok(new { success = result.Success, message = result.Message });
    }
}

public class HypertableCreateRequestDto
{
    public string TableName { get; set; } = "history_table";
    public string TimeColumn { get; set; } = "recorded_at";
    public string TimeInterval { get; set; } = "1 day";
    public bool MigrateData { get; set; } = true;
    public bool SetRetentionPolicy { get; set; } = true;
    public string RetentionPeriod { get; set; } = "90 days";
    public bool EnableCompression { get; set; } = true;
    public string CompressionPeriod { get; set; } = "1 day";
    public bool CreateContinuousAggregate { get; set; } = true;
}
