using UniVerse.Application.DTOs.Sync;
using System.Text.Json.Serialization;

namespace UniVerse.Application.Interfaces;

public interface IScheduleSyncService
{
    Task<SyncResultDto> SyncScheduleAsync(SyncScheduleRequest request);
    Task<SyncResultDto> SyncRoomsAsync();
    Task<List<EmployeeDto>> SearchEmployeesAsync(string fullname);
    Task<SyncStatusDto> GetLastSyncStatusAsync();
}

public interface IModeusApiClient
{
    Task<ModeusEventsResponse> SearchEventsAsync(SyncScheduleRequest request);
    Task<ModeusRoomsResponse> SearchRoomsAsync();
    Task<List<ModeusEmployee>> SearchEmployeeAsync(string fullname);
    Task<string?> GetSubIdByFullNameAsync(string fullname, CancellationToken cancellationToken = default);
}

// Modeus API response models
public class ModeusEvent
{
    public string Id { get; init; } = string.Empty;
    public string Name { get; init; } = string.Empty;
    public string? NameShort { get; init; }
    public string? Description { get; init; }
    public string? TypeId { get; init; }
    public DateTime StartsAt { get; init; }
    public DateTime EndsAt { get; init; }
    public ModeusIctisStats? IctisStats { get; init; }

    [JsonPropertyName("_links")]
    public ModeusEventLinks? Links { get; init; }
}

public record ModeusIctisStats(int? StudentCount, int? TeacherCount);

public class ModeusEventLinks
{
    [JsonPropertyName("course-unit-realization")]
    public ModeusHrefLink? CourseUnitRealization { get; init; }
}

public class ModeusEventsResponse
{
    [JsonPropertyName("_embedded")]
    public ModeusEventsEmbedded? Embedded { get; init; }
    public List<ModeusEvent>? Events { get; init; }
    public ModeusPage? Page { get; init; }

    [JsonIgnore]
    public IReadOnlyList<ModeusEvent> EventItems => Embedded?.Events ?? Events ?? [];
}
public class ModeusEventsEmbedded
{
    public List<ModeusEvent>? Events { get; init; }

    [JsonPropertyName("course-unit-realizations")]
    public List<ModeusCourseUnitRealization>? CourseUnitRealizations { get; init; }

    [JsonPropertyName("event-rooms")]
    public List<ModeusEventRoom>? EventRooms { get; init; }

    [JsonPropertyName("event-teams")]
    public List<ModeusEventTeam>? EventTeams { get; init; }

    [JsonPropertyName("event-attendees")]
    public List<ModeusEventAttendee>? EventAttendees { get; init; }

    public List<ModeusPerson>? Persons { get; init; }

    public List<ModeusRoom>? Rooms { get; init; }
}
public record ModeusHrefLink(string? Href);
public record ModeusCourseUnitRealization(string Id, string Name, string? NameShort);
public class ModeusEventRoom
{
    public string Id { get; init; } = string.Empty;

    [JsonPropertyName("_links")]
    public ModeusEventRoomLinks? Links { get; init; }
}
public class ModeusEventRoomLinks
{
    public ModeusHrefLink? Event { get; init; }
    public ModeusHrefLink? Room { get; init; }
}
public record ModeusEventTeam(string EventId, int? Size);
public class ModeusEventAttendee
{
    public string Id { get; init; } = string.Empty;
    public string? RoleId { get; init; }
    public string? RoleName { get; init; }

    [JsonPropertyName("_links")]
    public ModeusEventAttendeeLinks? Links { get; init; }
}
public class ModeusEventAttendeeLinks
{
    public ModeusHrefLink? Event { get; init; }
    public ModeusHrefLink? Person { get; init; }
}
public record ModeusPerson(string Id, string? LastName, string? FirstName, string? MiddleName, string? FullName);
public record ModeusBuilding(string? Id, string? Name, string? NameShort, string? Address);
public record ModeusRoom(string Id, string Name, string? NameShort, ModeusBuilding? Building, int? TotalCapacity, int? WorkingCapacity);
public record ModeusRoomsEmbedded(List<ModeusRoom>? Rooms);
public record ModeusPage(int Size, int TotalElements, int TotalPages, int Number);
public class ModeusRoomsResponse
{
    [JsonPropertyName("_embedded")]
    public ModeusRoomsEmbedded? Embedded { get; init; }
    public ModeusPage? Page { get; init; }
    public List<ModeusRoom>? Rooms { get; init; }

    [JsonIgnore]
    public IReadOnlyList<ModeusRoom> RoomItems => Embedded?.Rooms ?? Rooms ?? [];
}
public record ModeusEmployee(string? Id, string FullName, string? Department);
