using JobTrackerApi.Models;
using JobTrackerApi.Services;
using JobTrackerApi.Tests.TestSupport;
using Xunit;

namespace JobTrackerApi.Tests;

public sealed class AnalyticsSalaryTests
{
    [Fact]
    public async Task Salary_insights_keep_currency_and_period_groups_separate()
    {
        await using var db = TestHostFactory.CreateInMemoryDb("user-1");
        var company = new Company { Name = "Acme", OwnerUserId = "user-1" };
        db.Companies.Add(company);
        db.JobApplications.AddRange(
            new JobApplication { OwnerUserId = "user-1", Company = company, JobTitle = "A", Status = "Applied", SalaryMin = 600_000, SalaryMax = 800_000, SalaryCurrency = "NOK", SalaryPeriod = "year" },
            new JobApplication { OwnerUserId = "user-1", Company = company, JobTitle = "B", Status = "Applied", SalaryMin = 700_000, SalaryMax = 900_000, SalaryCurrency = "NOK", SalaryPeriod = "year" },
            new JobApplication { OwnerUserId = "user-1", Company = company, JobTitle = "C", Status = "Applied", SalaryMin = 50, SalaryMax = 70, SalaryCurrency = "EUR", SalaryPeriod = "hour" });
        await db.SaveChangesAsync();

        var result = await new AnalyticsService(db).GetAnalyticsOverviewAsync(default);

        Assert.Equal(2, result.SalaryInsights.Count);
        var nok = Assert.Single(result.SalaryInsights, x => x.Currency == "NOK" && x.Period == "year");
        Assert.Equal(2, nok.Count);
        Assert.Equal(600_000, nok.Minimum);
        Assert.Equal(900_000, nok.Maximum);
        Assert.Equal(750_000, nok.AverageMidpoint);
    }
}
