using System.Net;
using System.Net.Http.Json;
using CashLineERP.Contracts.Inventory;

namespace CashLineERP.Api.Tests;

public sealed class ReplenishmentOperationalHandoffQaTests : IClassFixture<ApiTestFactory>
{
    private readonly HttpClient _client;

    public ReplenishmentOperationalHandoffQaTests(ApiTestFactory factory)
    {
        _client = factory.CreateClient();
    }

    [Fact]
    public async Task Replenishment_Plan_Handoff_Endpoint_Creates_Draft_Transfer_And_Requisition()
    {
        await _client.AuthenticateAsInternalAsync();

        var createTransferRuleResponse = await _client.PostAsJsonAsync("/api/v1/inventory/replenishment-rules", new CreateReplenishmentRuleRequest(
            RuleName: "HTTP transfer handoff rule",
            BranchId: 1,
            TargetWarehouseId: 2,
            PreferredSourceWarehouseId: 1,
            ItemId: 1001,
            UnitOfMeasureCode: "EA",
            ReorderPointQuantity: 10m,
            TargetSafetyStockQuantity: 12m,
            TargetStockQuantity: 20m,
            SourceSafetyStockQuantity: 15m,
            Priority: 110,
            EffectiveFromDate: new DateOnly(2026, 5, 1),
            EffectiveToDate: new DateOnly(2026, 5, 31),
            Notes: "HTTP transfer handoff"));
        Assert.Equal(HttpStatusCode.OK, createTransferRuleResponse.StatusCode);
        var transferRule = await createTransferRuleResponse.Content.ReadFromJsonAsync<ReplenishmentRuleResponse>();
        Assert.NotNull(transferRule);

        Assert.Equal(HttpStatusCode.OK, (await _client.PostAsync($"/api/v1/inventory/replenishment-rules/{transferRule!.ReplenishmentRuleId}/submit", null)).StatusCode);

        await _client.AuthenticateAsFinanceCheckerAsync();
        Assert.Equal(HttpStatusCode.OK, (await _client.PostAsync($"/api/v1/inventory/replenishment-rules/{transferRule.ReplenishmentRuleId}/approve", null)).StatusCode);
        Assert.Equal(HttpStatusCode.OK, (await _client.PostAsync($"/api/v1/inventory/replenishment-rules/{transferRule.ReplenishmentRuleId}/activate", null)).StatusCode);

        await _client.AuthenticateAsInternalAsync();

        var createProcurementRuleResponse = await _client.PostAsJsonAsync("/api/v1/inventory/replenishment-rules", new CreateReplenishmentRuleRequest(
            RuleName: "HTTP procurement handoff rule",
            BranchId: 1,
            TargetWarehouseId: 2,
            PreferredSourceWarehouseId: 1,
            ItemId: 1002,
            UnitOfMeasureCode: "BOX",
            ReorderPointQuantity: 5m,
            TargetSafetyStockQuantity: 6m,
            TargetStockQuantity: 12m,
            SourceSafetyStockQuantity: 100m,
            Priority: 111,
            EffectiveFromDate: new DateOnly(2026, 5, 1),
            EffectiveToDate: new DateOnly(2026, 5, 31),
            Notes: "HTTP procurement handoff"));
        Assert.Equal(HttpStatusCode.OK, createProcurementRuleResponse.StatusCode);
        var procurementRule = await createProcurementRuleResponse.Content.ReadFromJsonAsync<ReplenishmentRuleResponse>();
        Assert.NotNull(procurementRule);

        Assert.Equal(HttpStatusCode.OK, (await _client.PostAsync($"/api/v1/inventory/replenishment-rules/{procurementRule!.ReplenishmentRuleId}/submit", null)).StatusCode);

        await _client.AuthenticateAsFinanceCheckerAsync();
        Assert.Equal(HttpStatusCode.OK, (await _client.PostAsync($"/api/v1/inventory/replenishment-rules/{procurementRule.ReplenishmentRuleId}/approve", null)).StatusCode);
        Assert.Equal(HttpStatusCode.OK, (await _client.PostAsync($"/api/v1/inventory/replenishment-rules/{procurementRule.ReplenishmentRuleId}/activate", null)).StatusCode);

        await _client.AuthenticateAsInternalAsync();

        var handoffResponse = await _client.PostAsJsonAsync("/api/v1/inventory/replenishment-rules/plan/handoff", new ReorderPlanningRequest(
            BranchId: 1,
            AsOfDate: new DateOnly(2026, 5, 21),
            TargetWarehouseId: 2,
            ItemId: null));
        Assert.Equal(HttpStatusCode.OK, handoffResponse.StatusCode);

        var handoff = await handoffResponse.Content.ReadFromJsonAsync<ReorderPlanHandoffResponse>();
        Assert.NotNull(handoff);
        Assert.NotEmpty(handoff!.TransferDrafts);
        Assert.NotEmpty(handoff.ProcurementDrafts);
    }
}
