using System.Threading.Channels;
using ErsatzTV.Application.Channels;
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Errors;
using ErsatzTV.Core.Scheduling;
using ErsatzTV.Infrastructure.Data;
using ErsatzTV.Infrastructure.Extensions;
using Microsoft.EntityFrameworkCore;
using Channel = ErsatzTV.Core.Domain.Channel;

namespace ErsatzTV.Application.Playouts;

public class CreateClassicPlayoutHandler : IRequestHandler<CreateClassicPlayout, Either<BaseError, CreatePlayoutResponse>>
{
    private readonly ChannelWriter<IBackgroundServiceRequest> _channel;
    private readonly IDbContextFactory<TvContext> _dbContextFactory;

    public CreateClassicPlayoutHandler(
        ChannelWriter<IBackgroundServiceRequest> channel,
        IDbContextFactory<TvContext> dbContextFactory)
    {
        _channel = channel;
        _dbContextFactory = dbContextFactory;
    }

    public async Task<Either<BaseError, CreatePlayoutResponse>> Handle(
        CreateClassicPlayout request,
        CancellationToken cancellationToken)
    {
        await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
        Option<Channel> maybeChannel = await GetChannel(dbContext, request, cancellationToken);
        return await maybeChannel.Match(
            Some: async channel =>
            {
                Option<ProgramSchedule> maybeProgramSchedule =
                    await GetProgramSchedule(dbContext, request, cancellationToken);
                return await maybeProgramSchedule.Match(
                    Some: async programSchedule =>
                    {
                        Validation<BaseError, Playout> validation = Validate(request, channel, programSchedule);
                        return await validation.Apply(playout => PersistPlayout(dbContext, playout));
                    },
                    None: () => Task.FromResult<Either<BaseError, CreatePlayoutResponse>>(
                        new NotFoundError("Program schedule does not exist")));
            },
            None: () => Task.FromResult<Either<BaseError, CreatePlayoutResponse>>(
                new NotFoundError("Channel does not exist")));
    }

    private async Task<CreatePlayoutResponse> PersistPlayout(TvContext dbContext, Playout playout)
    {
        await dbContext.Playouts.AddAsync(playout);
        await dbContext.SaveChangesAsync();
        await _channel.WriteAsync(new BuildPlayout(playout.Id, PlayoutBuildMode.Reset));
        if (playout.Channel.PlayoutMode is ChannelPlayoutMode.OnDemand)
        {
            await _channel.WriteAsync(new TimeShiftOnDemandPlayout(playout.Id, DateTimeOffset.Now, false));
        }

        await _channel.WriteAsync(new RefreshChannelList());
        return new CreatePlayoutResponse(playout.Id);
    }

    private static Validation<BaseError, Playout> Validate(
        CreateClassicPlayout request,
        Channel channel,
        ProgramSchedule programSchedule) =>
        (ChannelMustNotHavePlayouts(channel),
            ProgramScheduleMustHaveItems(programSchedule),
            ValidateScheduleKind(request))
        .Apply((channel, programSchedule, scheduleKind) => new Playout
        {
            ChannelId = channel.Id,
            ProgramScheduleId = programSchedule.Id,
            ScheduleKind = scheduleKind
        });

    private static Task<Option<Channel>> GetChannel(
        TvContext dbContext,
        CreateClassicPlayout createClassicPlayout,
        CancellationToken cancellationToken) =>
        dbContext.Channels
            .Include(c => c.Playouts)
            .SelectOneAsync(c => c.Id, c => c.Id == createClassicPlayout.ChannelId, cancellationToken);

    private static Validation<BaseError, Channel> ChannelMustNotHavePlayouts(Channel channel) =>
        Optional(channel.Playouts.Count)
            .Filter(count => count == 0)
            .Map(_ => channel)
            .ToValidation<BaseError>("Channel already has one playout");

    private static Task<Option<ProgramSchedule>> GetProgramSchedule(
        TvContext dbContext,
        CreateClassicPlayout createClassicPlayout,
        CancellationToken cancellationToken) =>
        dbContext.ProgramSchedules
            .Include(ps => ps.Items)
            .SelectOneAsync(ps => ps.Id, ps => ps.Id == createClassicPlayout.ProgramScheduleId, cancellationToken);

    private static Validation<BaseError, ProgramSchedule> ProgramScheduleMustHaveItems(
        ProgramSchedule programSchedule) =>
        Optional(programSchedule)
            .Filter(ps => ps.Items.Count != 0)
            .ToValidation<BaseError>("Program schedule must have items");

    private static Validation<BaseError, PlayoutScheduleKind> ValidateScheduleKind(
        CreateClassicPlayout createClassicPlayout) =>
        Optional(createClassicPlayout.ScheduleKind)
            .Filter(scheduleKind => scheduleKind == PlayoutScheduleKind.Classic)
            .ToValidation<BaseError>("[ScheduleKind] must be Classic");
}
