using System.Net;
using System.Net.Http.Json;
using CashLineERP.Contracts.External;
using CashLineERP.Contracts.Pricing;

namespace CashLineERP.Api.Tests;

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

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

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

        var createRuleResponse = await _client.PostAsJsonAsync("/api/v1/sales/pricing-rules", new CreatePricingRuleRequest(
            RuleName: "External customer price rule",
            BranchId: 1,
            CustomerId: 2,
            ItemId: 1001,
            CurrencyCode: "EGP",
            UnitOfMeasureCode: "EA",
            MinimumQuantity: 4m,
            MaximumQuantity: null,
            FixedUnitPrice: 26m,
            Priority: 125,
            EffectiveFromDate: new DateOnly(2026, 5, 1),
            EffectiveToDate: new DateOnly(2026, 5, 31),
            Notes: "External pricing visibility baseline"));
        Assert.Equal(HttpStatusCode.OK, createRuleResponse.StatusCode);

        var rule = await createRuleResponse.Content.ReadFromJsonAsync<PricingRuleResponse>();
        Assert.NotNull(rule);

        var submitResponse = await _client.PostAsync($"/api/v1/sales/pricing-rules/{rule!.PricingRuleId}/submit", null);
        Assert.Equal(HttpStatusCode.OK, submitResponse.StatusCode);

        await _client.AuthenticateAsFinanceCheckerAsync();

        Assert.Equal(HttpStatusCode.OK, (await _client.PostAsync($"/api/v1/sales/pricing-rules/{rule.PricingRuleId}/approve", null)).StatusCode);
        Assert.Equal(HttpStatusCode.OK, (await _client.PostAsync($"/api/v1/sales/pricing-rules/{rule.PricingRuleId}/activate", null)).StatusCode);

        await _client.AuthenticateAsCustomerAsync();

        var evaluateResponse = await _client.PostAsJsonAsync("/api/v1/external/parties/2/pricing/evaluate", new ExternalPricingEvaluationRequest(
            BranchId: 1,
            PricingDate: new DateOnly(2026, 5, 21),
            CurrencyCode: "EGP",
            Lines:
            [
                new ExternalPricingEvaluationLineRequest(1001, 6m, "EA", null)
            ]));
        Assert.Equal(HttpStatusCode.OK, evaluateResponse.StatusCode);

        var evaluation = await evaluateResponse.Content.ReadFromJsonAsync<PricingEvaluationResponse>();
        Assert.NotNull(evaluation);
        Assert.Equal(2L, evaluation!.CustomerId);
        var line = Assert.Single(evaluation.Lines);
        Assert.Equal("RULE", line.PricingSourceCode);
        Assert.Equal(26m, line.AppliedUnitPrice);

        var forbiddenResponse = await _client.PostAsJsonAsync("/api/v1/external/parties/1/pricing/evaluate", new ExternalPricingEvaluationRequest(
            BranchId: 1,
            PricingDate: new DateOnly(2026, 5, 21),
            CurrencyCode: "EGP",
            Lines:
            [
                new ExternalPricingEvaluationLineRequest(1001, 6m, "EA", null)
            ]));
        Assert.Equal(HttpStatusCode.Forbidden, forbiddenResponse.StatusCode);
    }
}
