using System.Security.Claims;
using JobsMedical.Web.Data;
using JobsMedical.Web.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;

namespace JobsMedical.Web.Pages.Employer;

[Authorize]
public class PostShiftModel : PageModel
{
    private readonly AppDbContext _db;
    public PostShiftModel(AppDbContext db) => _db = db;

    public List<Facility> MyFacilities { get; private set; } = new();
    public List<Role> Roles { get; private set; } = new();
    public string? Error { get; private set; }

    [BindProperty] public int FacilityId { get; set; }
    [BindProperty] public int RoleId { get; set; }
    [BindProperty] public DateOnly Date { get; set; }
    [BindProperty] public ShiftType ShiftType { get; set; }
    [BindProperty] public TimeOnly StartTime { get; set; }
    [BindProperty] public TimeOnly EndTime { get; set; }
    [BindProperty] public long? PayAmount { get; set; }
    [BindProperty] public int? SharePercent { get; set; }   // سهم درآمد (٪)
    [BindProperty] public bool Negotiable { get; set; }
    [BindProperty] public Gender GenderRequirement { get; set; }
    [BindProperty] public string? Description { get; set; }

    public async Task OnGetAsync()
    {
        await LoadListsAsync();
        Date = DateOnly.FromDateTime(DateTime.UtcNow).AddDays(1);
        StartTime = new TimeOnly(8, 0);
        EndTime = new TimeOnly(14, 0);
    }

    public async Task<IActionResult> OnPostAsync()
    {
        await LoadListsAsync();
        if (!MyFacilities.Any(f => f.Id == FacilityId))
        {
            Error = "این مرکز متعلق به شما نیست.";
            return Page();
        }
        var role = await _db.Roles.FindAsync(RoleId);
        _db.Shifts.Add(new Shift
        {
            FacilityId = FacilityId,
            RoleId = RoleId,
            Date = Date,
            StartTime = StartTime,
            EndTime = EndTime,
            ShiftType = ShiftType,
            SpecialtyRequired = role?.Name ?? "",
            Description = Description,
            PayType = Negotiable ? PayType.Negotiable
                : (PayAmount is null && SharePercent is not null ? PayType.Percentage : PayType.PerShift),
            PayAmount = Negotiable ? null : PayAmount,
            SharePercent = Negotiable ? null : SharePercent,
            GenderRequirement = GenderRequirement,
            Status = ShiftStatus.Open,
            Source = ShiftSource.Direct, // posted directly by the facility
        });
        await _db.SaveChangesAsync();
        return RedirectToPage("/Employer/Index");
    }

    private async Task LoadListsAsync()
    {
        var userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
        MyFacilities = await _db.Facilities.Include(f => f.City)
            .Where(f => f.OwnerUserId == userId).OrderBy(f => f.Name).ToListAsync();
        Roles = await _db.Roles.Where(r => r.IsActive).OrderBy(r => r.SortOrder).ToListAsync();
    }
}
