using System.Text.Json;
using Tiku.Application.Catalog;

namespace Tiku.Application.Scoreline;

public sealed record ScorelineFilter(
    Guid TenantId,
    Guid? RegionId = null,
    Guid? SchoolId = null,
    Guid? MajorId = null,
    string? Keyword = null,
    int? Year = null,
    int? Page = null,
    int? PageSize = null,
    int? Limit = null,
    IReadOnlyCollection<ScorelineDynamicFilter>? DynamicFilters = null);

public sealed record ScorelineDynamicFilter(string Operator, string FieldKey, string Value);

public sealed record ScorelineFieldItem(
    Guid Id,
    string? LegacyId,
    Guid? RegionId,
    string FieldKey,
    string FieldName,
    string FieldType,
    string? Unit,
    bool IsFilter,
    bool IsRequired,
    bool IsVisible,
    bool IsTrend,
    JsonElement Options,
    string? Placeholder,
    string? Description,
    int SortOrder);

public sealed record ScorelineRecordItem(
    Guid Id,
    string? LegacyId,
    Guid? RegionId,
    Guid? SchoolId,
    Guid? MajorId,
    int Year,
    string? SchoolName,
    string? MajorName,
    JsonElement FieldValues);

public sealed record ScorelineRecordPage(
    int Page,
    int PageSize,
    int Total,
    IReadOnlyCollection<ScorelineRecordItem> Items);

public sealed record ScorelineRecordCursorPage(
    int PageSize,
    bool HasMore,
    string? NextCursor,
    IReadOnlyCollection<ScorelineRecordItem> Items);

public interface IScorelineQueryService
{
    Task<CatalogList<ScorelineFieldItem>> GetFieldsAsync(ScorelineFilter filter, CancellationToken cancellationToken = default);
    Task<ScorelineRecordPage> GetRecordsAsync(ScorelineFilter filter, CancellationToken cancellationToken = default);
    Task<ScorelineRecordCursorPage> GetRecordsCursorAsync(ScorelineFilter filter, string? cursor, CancellationToken cancellationToken = default);
    Task<CatalogList<ScorelineRecordItem>> GetTrendAsync(ScorelineFilter filter, CancellationToken cancellationToken = default);
    Task<CatalogList<int>> GetYearsAsync(ScorelineFilter filter, CancellationToken cancellationToken = default);
}
