using System.Data;
using Dapper;
using PlotLine.Models;

namespace PlotLine.Data;

public interface ISubscriptionRepository
{
    Task<UserSubscriptionDetail?> GetCurrentSubscriptionAsync(int userId);
    Task<SubscriptionLevel?> GetSubscriptionLevelAsync(int subscriptionLevelId);
    Task<IReadOnlyList<SubscriptionLevel>> GetActiveSubscriptionLevelsAsync();
    Task<int> GetOwnedBookCountAsync(int userId);
    Task<int> GetChapterCountForBookAsync(int bookId);
    Task<int> GetSceneCountForBookAsync(int bookId);
    Task<int> GetCharacterCountForProjectAsync(int projectId);
    Task<int> GetCollaboratorCountForOwnerAsync(int userId);
}

public sealed class SubscriptionRepository(ISqlConnectionFactory connectionFactory) : ISubscriptionRepository
{
    public async Task<UserSubscriptionDetail?> GetCurrentSubscriptionAsync(int userId)
    {
        using var connection = connectionFactory.CreateConnection();
        return await connection.QuerySingleOrDefaultAsync<UserSubscriptionDetail>(
            "dbo.UserSubscription_GetCurrent",
            new { UserID = userId },
            commandType: CommandType.StoredProcedure);
    }

    public async Task<SubscriptionLevel?> GetSubscriptionLevelAsync(int subscriptionLevelId)
    {
        using var connection = connectionFactory.CreateConnection();
        return await connection.QuerySingleOrDefaultAsync<SubscriptionLevel>(
            "dbo.SubscriptionLevel_Get",
            new { SubscriptionLevelID = subscriptionLevelId },
            commandType: CommandType.StoredProcedure);
    }

    public async Task<IReadOnlyList<SubscriptionLevel>> GetActiveSubscriptionLevelsAsync()
    {
        using var connection = connectionFactory.CreateConnection();
        var rows = await connection.QueryAsync<SubscriptionLevel>(
            "dbo.SubscriptionLevel_ListActive",
            commandType: CommandType.StoredProcedure);
        return rows.ToList();
    }

    public async Task<int> GetOwnedBookCountAsync(int userId)
    {
        using var connection = connectionFactory.CreateConnection();
        return await connection.QuerySingleAsync<int>(
            "dbo.SubscriptionUsage_BookCountForOwner",
            new { UserID = userId },
            commandType: CommandType.StoredProcedure);
    }

    public async Task<int> GetChapterCountForBookAsync(int bookId)
    {
        using var connection = connectionFactory.CreateConnection();
        return await connection.QuerySingleAsync<int>(
            "dbo.SubscriptionUsage_ChapterCountForBook",
            new { BookID = bookId },
            commandType: CommandType.StoredProcedure);
    }

    public async Task<int> GetSceneCountForBookAsync(int bookId)
    {
        using var connection = connectionFactory.CreateConnection();
        return await connection.QuerySingleAsync<int>(
            "dbo.SubscriptionUsage_SceneCountForBook",
            new { BookID = bookId },
            commandType: CommandType.StoredProcedure);
    }

    public async Task<int> GetCharacterCountForProjectAsync(int projectId)
    {
        using var connection = connectionFactory.CreateConnection();
        return await connection.QuerySingleAsync<int>(
            "dbo.SubscriptionUsage_CharacterCountForProject",
            new { ProjectID = projectId },
            commandType: CommandType.StoredProcedure);
    }

    public async Task<int> GetCollaboratorCountForOwnerAsync(int userId)
    {
        using var connection = connectionFactory.CreateConnection();
        return await connection.QuerySingleAsync<int>(
            "dbo.SubscriptionUsage_CollaboratorCountForOwner",
            new { UserID = userId },
            commandType: CommandType.StoredProcedure);
    }
}
