using Dapper;
using Npgsql;
using SatelliteProvider.Common.Enums;
using SatelliteProvider.DataAccess.Models;

namespace SatelliteProvider.DataAccess.Repositories;

public class RegionRepository : IRegionRepository
{
    private const string ColumnList = @"id, latitude, longitude, size_meters as SizeMeters,
                   zoom_level as ZoomLevel, status,
                   csv_file_path as CsvFilePath, summary_file_path as SummaryFilePath,
                   tiles_downloaded as TilesDownloaded, tiles_reused as TilesReused,
                   stitch_tiles as StitchTiles,
                   created_at as CreatedAt, updated_at as UpdatedAt";

    private readonly string _connectionString;

    public RegionRepository(string connectionString)
    {
        _connectionString = connectionString;
    }

    public async Task<RegionEntity?> GetByIdAsync(Guid id)
    {
        using var connection = new NpgsqlConnection(_connectionString);
        const string sql = $@"
            SELECT {ColumnList}
            FROM regions
            WHERE id = @Id";

        var region = await connection.QuerySingleOrDefaultAsync<RegionEntity>(sql, new { Id = id });
        return region;
    }

    public async Task<IEnumerable<RegionEntity>> GetByStatusAsync(RegionStatus status)
    {
        using var connection = new NpgsqlConnection(_connectionString);
        const string sql = $@"
            SELECT {ColumnList}
            FROM regions
            WHERE status = @Status
            ORDER BY created_at ASC";

        return await connection.QueryAsync<RegionEntity>(sql, new { Status = status });
    }

    public async Task<Guid> InsertAsync(RegionEntity region)
    {
        using var connection = new NpgsqlConnection(_connectionString);
        const string sql = @"
            INSERT INTO regions (id, latitude, longitude, size_meters, zoom_level, 
                               status, csv_file_path, summary_file_path,
                               tiles_downloaded, tiles_reused, stitch_tiles, 
                               created_at, updated_at)
            VALUES (@Id, @Latitude, @Longitude, @SizeMeters, @ZoomLevel, 
                    @Status, @CsvFilePath, @SummaryFilePath,
                    @TilesDownloaded, @TilesReused, @StitchTiles,
                    @CreatedAt, @UpdatedAt)
            RETURNING id";

        return await connection.ExecuteScalarAsync<Guid>(sql, region);
    }

    public async Task<int> UpdateAsync(RegionEntity region)
    {
        using var connection = new NpgsqlConnection(_connectionString);
        const string sql = @"
            UPDATE regions 
            SET latitude = @Latitude,
                longitude = @Longitude,
                size_meters = @SizeMeters,
                zoom_level = @ZoomLevel,
                status = @Status,
                csv_file_path = @CsvFilePath,
                summary_file_path = @SummaryFilePath,
                tiles_downloaded = @TilesDownloaded,
                tiles_reused = @TilesReused,
                stitch_tiles = @StitchTiles,
                updated_at = @UpdatedAt
            WHERE id = @Id";

        return await connection.ExecuteAsync(sql, region);
    }

    public async Task<int> DeleteAsync(Guid id)
    {
        using var connection = new NpgsqlConnection(_connectionString);
        const string sql = "DELETE FROM regions WHERE id = @Id";
        return await connection.ExecuteAsync(sql, new { Id = id });
    }
}
