using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using QuantEngine.Core.Interfaces;
using QuantEngine.Infrastructure.Data;

namespace QuantEngine.Infrastructure.Repositories
{
    public class CollectionRepository : ICollectionReadRepository, ICollectionWriteRepository
    {
        private readonly IDbConnectionFactory _connectionFactory;

        public CollectionRepository(IDbConnectionFactory connectionFactory)
        {
            _connectionFactory = connectionFactory;
        }

        private async Task ExecuteAsync(string sql, object? param = null)
        {
            using var conn = _connectionFactory.CreateConnection();
            await conn.ExecuteAsync(sql, param);
        }

        private async Task<List<T>> QueryListAsync<T>(string sql, object? param = null)
        {
            using var conn = _connectionFactory.CreateConnection();
            return (await conn.QueryAsync<T>(sql, param)).ToList();
        }

        private async Task<T?> QuerySingleOrDefaultAsync<T>(string sql, object? param = null)
        {
            using var conn = _connectionFactory.CreateConnection();
            return await conn.QueryFirstOrDefaultAsync<T>(sql, param);
        }

        public async Task SaveRunAsync(CollectionRunRecord run)
        {
            await ExecuteAsync(@"
                INSERT INTO quantengine.kis_collection_runs (run_id, status, started_at, finished_at, total_snapshots, total_errors, updated_at)
                VALUES (@RunId, @Status, @StartedAt, @FinishedAt, @TotalSnapshots, @TotalErrors, @UpdatedAt)
                ON CONFLICT (run_id) DO UPDATE SET
                    status = EXCLUDED.status,
                    finished_at = EXCLUDED.finished_at,
                    total_snapshots = EXCLUDED.total_snapshots,
                    total_errors = EXCLUDED.total_errors,
                    updated_at = EXCLUDED.updated_at",
                run
            );
        }

        public async Task UpdateRunStatusAsync(string runId, string status, string? finishedAt = null, int? totalSnapshots = null, int? totalErrors = null)
        {
            await ExecuteAsync(@"
                UPDATE quantengine.kis_collection_runs
                SET status = @Status, finished_at = @FinishedAt, total_snapshots = @TotalSnapshots, total_errors = @TotalErrors, updated_at = @UpdatedAt
                WHERE run_id = @RunId",
                new { RunId = runId, Status = status, FinishedAt = finishedAt, TotalSnapshots = totalSnapshots, TotalErrors = totalErrors, UpdatedAt = DateTime.UtcNow.ToString("o") }
            );
        }

        public async Task SaveSnapshotAsync(CollectionSnapshotRecord snapshot)
        {
            await ExecuteAsync(@"
                INSERT INTO quantengine.kis_collection_snapshots (run_id, dataset_name, ticker, source_name, payload_json, captured_at, created_at)
                VALUES (@RunId, @DatasetName, @Ticker, @SourceName, @PayloadJson, @CapturedAt, @CreatedAt)
                ON CONFLICT (run_id, ticker, source_name) DO UPDATE SET
                    payload_json = EXCLUDED.payload_json,
                    captured_at = EXCLUDED.captured_at",
                snapshot
            );
        }

        public async Task SaveErrorAsync(CollectionErrorRecord error)
        {
            await ExecuteAsync(@"
                INSERT INTO quantengine.kis_collection_errors (run_id, source_name, error_kind, error_message, ticker, created_at)
                VALUES (@RunId, @SourceName, @ErrorKind, @ErrorMessage, @Ticker, @CreatedAt)",
                error
            );
        }

        public async Task<List<CollectionRunRecord>> GetRecentRunsAsync(int limit = 20)
        {
            return await QueryListAsync<CollectionRunRecord>(@"
                SELECT run_id as RunId, status, started_at as StartedAt, finished_at as FinishedAt,
                       total_snapshots as TotalSnapshots, total_errors as TotalErrors, updated_at as UpdatedAt
                FROM quantengine.kis_collection_runs
                ORDER BY started_at DESC
                LIMIT @Limit",
                new { Limit = limit }
            );
        }

        public async Task<List<CollectionSnapshotRecord>> GetRunSnapshotsAsync(string runId)
        {
            return await QueryListAsync<CollectionSnapshotRecord>(@"
                SELECT run_id as RunId, dataset_name as DatasetName, ticker, source_name as SourceName,
                       payload_json as PayloadJson, captured_at as CapturedAt, created_at as CreatedAt
                FROM quantengine.kis_collection_snapshots
                WHERE run_id = @RunId
                ORDER BY captured_at DESC",
                new { RunId = runId }
            );
        }

        public async Task<List<CollectionErrorRecord>> GetRunErrorsAsync(string runId, int limit = 50)
        {
            return await QueryListAsync<CollectionErrorRecord>(@"
                SELECT run_id as RunId, source_name as SourceName, error_kind as ErrorKind,
                       error_message as ErrorMessage, ticker as Ticker, created_at as CreatedAt
                FROM quantengine.kis_collection_errors
                WHERE run_id = @RunId
                ORDER BY created_at DESC
                LIMIT @Limit",
                new { RunId = runId, Limit = limit }
            );
        }

        public async Task<CollectionDashboardStateRecord> GetDashboardStateAsync()
        {
            var lastRun = await QuerySingleOrDefaultAsync<CollectionRunRecord>(@"
                SELECT run_id as RunId, status, started_at as StartedAt, finished_at as FinishedAt,
                       total_snapshots as TotalSnapshots, total_errors as TotalErrors, updated_at as UpdatedAt
                FROM quantengine.kis_collection_runs
                ORDER BY started_at DESC
                LIMIT 1");

            var stats = await QuerySingleOrDefaultAsync<dynamic>(@"
                SELECT
                    COALESCE(SUM(total_snapshots), 0) as TotalSnapshots,
                    COALESCE(SUM(total_errors), 0) as TotalErrors
                FROM quantengine.kis_collection_runs");

            var recentErrors = await QueryListAsync<CollectionErrorRecord>(@"
                SELECT run_id as RunId, source_name as SourceName, error_kind as ErrorKind,
                       error_message as ErrorMessage, ticker as Ticker, created_at as CreatedAt
                FROM quantengine.kis_collection_errors
                ORDER BY created_at DESC
                LIMIT 5");

            return new CollectionDashboardStateRecord(
                LastRunId: lastRun?.RunId,
                LastRunStatus: lastRun?.Status,
                LastFinishedAt: lastRun?.FinishedAt,
                TotalSnapshots: stats?.TotalSnapshots ?? 0,
                TotalErrors: stats?.TotalErrors ?? 0,
                RecentErrors: recentErrors
            );
        }

        public async Task<List<CollectionSnapshotRecord>> GetLatestSnapshotsForTickerAsync(string ticker, int limit = 10)
        {
            return await QueryListAsync<CollectionSnapshotRecord>(@"
                SELECT run_id as RunId, dataset_name as DatasetName, ticker, source_name as SourceName,
                       payload_json as PayloadJson, captured_at as CapturedAt, created_at as CreatedAt
                FROM quantengine.kis_collection_snapshots
                WHERE ticker = @Ticker
                ORDER BY captured_at DESC
                LIMIT @Limit",
                new { Ticker = ticker, Limit = limit }
            );
        }

        public async Task SavePriceHistoryDailyAsync(PriceHistoryDailyRecord record)
        {
            await ExecuteAsync(@"
                INSERT INTO quantengine.price_history_daily (ticker, trade_date, open, high, low, close, volume, source, provenance)
                VALUES (@Ticker, @TradeDate, @Open, @High, @Low, @Close, @Volume, @Source, @Provenance::jsonb)
                ON CONFLICT (ticker, trade_date) DO NOTHING",
                new
                {
                    record.Ticker,
                    // Dapper has no built-in type handler for System.DateOnly (throws
                    // NotSupportedException) — pass as DateTime; the DATE column truncates the time part.
                    TradeDate = record.TradeDate.ToDateTime(TimeOnly.MinValue),
                    record.Open,
                    record.High,
                    record.Low,
                    record.Close,
                    record.Volume,
                    record.Source,
                    Provenance = record.ProvenanceJson ?? "{}"
                }
            );
        }

        public async Task<List<PriceHistorySummaryRecord>> GetPriceHistorySummaryAsync()
        {
            return await QueryListAsync<PriceHistorySummaryRecord>(@"
                SELECT ticker AS Ticker, count(*)::int AS RowCount, min(trade_date) AS FirstDate, max(trade_date) AS LastDate
                FROM quantengine.price_history_daily
                GROUP BY ticker
                ORDER BY ticker",
                new { }
            );
        }

    }
}
