using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PlanTempus.Application.Features.Employees.Components;

namespace PlanTempus.Application.Features.Employees.Pages;

public class SalarySpecificationModel : PageModel
{
    private readonly IWebHostEnvironment _environment;

    public SalarySpecificationModel(IWebHostEnvironment environment)
    {
        _environment = environment;
    }

    public SalarySpecificationDto? Specification { get; private set; }
    public string EmployeeName { get; private set; } = "Emma Larsen";
    public string EmployeeNumber { get; private set; } = "EMP-001";
    public string Department { get; private set; } = "Frisør";
    public string EmploymentType { get; private set; } = "Fuldtid (37 t/uge)";

    public IActionResult OnGet(string period)
    {
        var specs = LoadSalarySpecifications();
        Specification = specs.FirstOrDefault(s => s.PeriodKey == period);

        if (Specification == null)
        {
            return NotFound();
        }

        return Page();
    }

    private List<SalarySpecificationDto> LoadSalarySpecifications()
    {
        var jsonPath = Path.Combine(_environment.ContentRootPath, "Features", "Employees", "Data", "salarySpecificationMock.json");
        var json = System.IO.File.ReadAllText(jsonPath);
        var root = JsonSerializer.Deserialize<SalarySpecificationRoot>(json, new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true
        });
        return root?.Specifications ?? new List<SalarySpecificationDto>();
    }
}
