using Baya.Domain.Entities.Holidays;
using Baya.Infrastructure.Persistence.Services.Holidays;

namespace Baya.Test.Foundation.Marketplace;

public sealed class HolidayCalendarServiceTests
{
    [Fact]
    public async Task IsBankClosed_SeededBankClosedDate_True()
    {
        using var host = new OpsTestHost();
        var calendar = new HolidayCalendarService(host.Db, host.Cache);

        Assert.True(await calendar.IsBankClosed(new DateOnly(2026, 3, 21)));
    }

    [Fact]
    public async Task IsHoliday_NonHoliday_False()
    {
        using var host = new OpsTestHost();
        var calendar = new HolidayCalendarService(host.Db, host.Cache);

        Assert.False(await calendar.IsHoliday(new DateOnly(2026, 5, 4)));
    }

    [Fact]
    public async Task NextBusinessDay_FromHoliday_ReturnsOpenBankDay()
    {
        using var host = new OpsTestHost();
        var calendar = new HolidayCalendarService(host.Db, host.Cache);

        // The Nowruz block 21–24 March is bank-closed; the answer is the first later open, non-Friday day.
        var next = await calendar.NextBusinessDay(new DateOnly(2026, 3, 21));

        Assert.True(next > new DateOnly(2026, 3, 24));
        Assert.NotEqual(DayOfWeek.Friday, next.DayOfWeek);
        Assert.False(await calendar.IsBankClosed(next));
    }

    [Fact]
    public async Task Upsert_ThenDelete_RoundTrips()
    {
        using var host = new OpsTestHost();
        host.CurrentUser.UserId = await host.AddUserAsync("admin");
        var calendar = new HolidayCalendarService(host.Db, host.Cache);
        var date = new DateOnly(2026, 9, 1);

        await calendar.UpsertAsync(date, "روز آزمایشی", HolidayType.Official, true);
        Assert.True(await calendar.IsHoliday(date));
        Assert.True(await calendar.IsBankClosed(date));

        Assert.True(await calendar.DeleteAsync(date));
        Assert.False(await calendar.IsHoliday(date));
        Assert.False(await calendar.DeleteAsync(date));
    }
}
