<?php

namespace App\Notifications;

use App\Models\EmployeePayroll;
use App\Models\PayrollCycle;

class PayslipReadyNotification extends HrmsNotification
{
    public function __construct(
        public EmployeePayroll $employeePayroll,
        public PayrollCycle $cycle
    ) {}

    public function notificationCategory(): string
    {
        return 'payroll';
    }

    public function notificationSubject(): string
    {
        return 'Your payslip is ready';
    }

    public function notificationLines(): array
    {
        $label = $this->periodLabel();

        return [
            'Your payslip for ' . $label . ' is ready to view.',
        ];
    }

    public function actionUrl(): ?string
    {
        return route('payroll.my.payslips.show', $this->employeePayroll);
    }

    public function actionLabel(): string
    {
        return 'View payslip';
    }

    public function toArray(object $notifiable): array
    {
        return [
            'type' => 'payslip_ready',
            'message' => 'Your payslip for ' . $this->periodLabel() . ' is ready.',
            'employee_payroll_id' => $this->employeePayroll->id,
            'payroll_cycle_id' => $this->cycle->id,
            'month' => $this->cycle->month,
            'year' => $this->cycle->year,
            'url' => route('payroll.my.payslips.show', $this->employeePayroll),
        ];
    }

    protected function fcmType(): string
    {
        return 'payslip_ready';
    }

    protected function fcmRoute(): string
    {
        return '/payslip';
    }

    protected function fcmData(): array
    {
        return [
            'employee_payroll_id' => (string) $this->employeePayroll->id,
            'payroll_cycle_id' => (string) $this->cycle->id,
            'month' => (string) $this->cycle->month,
            'year' => (string) $this->cycle->year,
        ];
    }

    protected function periodLabel(): string
    {
        $month = (int) $this->cycle->month;
        $year = (int) $this->cycle->year;
        $name = date('F', mktime(0, 0, 0, max(1, min(12, $month)), 1));

        return $name . ' ' . $year;
    }
}
