<?php

namespace App\Notifications;

use App\Models\EmployeeImport;

class EmployeeImportCompletedNotification extends HrmsNotification
{
    public function __construct(
        public EmployeeImport $import,
        public bool $success,
        public ?string $errorMessage = null
    ) {}

    public function notificationCategory(): string
    {
        return 'payroll';
    }

    public function notificationSubject(): string
    {
        $file = $this->import->original_file_name ?? 'employee import';

        return $this->success
            ? 'Employee import completed — ' . $file
            : 'Employee import failed — ' . $file;
    }

    public function notificationLines(): array
    {
        if (! $this->success) {
            return [
                'Your employee import could not be completed.',
                $this->errorMessage ?? 'Please review the file and try again.',
            ];
        }

        return [
            'Your employee import has finished processing.',
            'Successful rows: ' . (int) $this->import->success_rows,
            'Failed rows: ' . (int) $this->import->failed_rows,
            'Total rows: ' . (int) $this->import->total_rows,
        ];
    }

    public function actionUrl(): ?string
    {
        return route('employees.import.show', $this->import);
    }

    public function actionLabel(): string
    {
        return 'View import results';
    }

    public function toArray(object $notifiable): array
    {
        return [
            'type' => $this->success ? 'employee_import_completed' : 'employee_import_failed',
            'message' => $this->success
                ? 'Employee import completed (' . (int) $this->import->success_rows . ' succeeded, ' . (int) $this->import->failed_rows . ' failed).'
                : 'Employee import failed.',
            'import_id' => $this->import->id,
            'success_rows' => $this->import->success_rows,
            'failed_rows' => $this->import->failed_rows,
            'error' => $this->errorMessage,
            'url' => route('employees.import.show', $this->import),
        ];
    }
}
