<?php

namespace App\Notifications;

use App\Models\AttendanceImport;

class AttendanceImportCompletedNotification extends HrmsNotification
{
    public function __construct(
        public AttendanceImport $import,
        public bool $success,
        public ?string $errorMessage = null
    ) {}

    public function notificationCategory(): string
    {
        return 'attendance';
    }

    public function notificationSubject(): string
    {
        $file = $this->import->original_file_name ?? 'attendance import';

        return $this->success
            ? 'Attendance import completed — '.$file
            : 'Attendance import failed — '.$file;
    }

    public function notificationLines(): array
    {
        if (! $this->success) {
            return [
                'Your attendance import could not be completed.',
                $this->errorMessage ?? 'Please review the file and try again.',
            ];
        }

        return [
            'Your attendance import has finished processing.',
            'Successfully imported: '.(int) $this->import->success_rows,
            'Duplicate records: '.(int) $this->import->duplicate_rows,
            'Failed records: '.(int) $this->import->failed_rows,
            'Total records: '.(int) $this->import->total_rows,
        ];
    }

    public function actionUrl(): ?string
    {
        return route('attendance.import.show', $this->import);
    }

    public function actionLabel(): string
    {
        return 'View import results';
    }

    public function toArray(object $notifiable): array
    {
        return [
            'type' => $this->success ? 'attendance_import_completed' : 'attendance_import_failed',
            'message' => $this->success
                ? 'Attendance import completed ('.(int) $this->import->success_rows.' imported, '.(int) $this->import->failed_rows.' failed).'
                : 'Attendance import failed.',
            'import_id' => $this->import->id,
            'success_rows' => $this->import->success_rows,
            'failed_rows' => $this->import->failed_rows,
            'duplicate_rows' => $this->import->duplicate_rows,
            'error' => $this->errorMessage,
            'url' => route('attendance.import.show', $this->import),
        ];
    }
}
