<?php

declare(strict_types=1);

namespace App\Models;

use App\Business\DataImport\Enums\DataImportBatchStatus;
use App\Business\DataImport\Enums\DataImportType;
use Database\Factories\DberpDataImportBatchFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class DberpDataImportBatch extends Model
{
    /** @use HasFactory<DberpDataImportBatchFactory> */
    use HasFactory;

    protected $table = 'dberp_data_import_batches';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'batch_no',
        'import_type',
        'status',
        'source_filename',
        'stored_path',
        'operator_user_id',
        'started_at',
        'finished_at',
        'total_rows',
        'success_rows',
        'failed_rows',
        'error_summary',
        'options',
        'correlation_id',
        'remark',
    ];

    protected function casts(): array
    {
        return [
            'import_type' => DataImportType::class,
            'status' => DataImportBatchStatus::class,
            'operator_user_id' => 'integer',
            'started_at' => 'datetime',
            'finished_at' => 'datetime',
            'total_rows' => 'integer',
            'success_rows' => 'integer',
            'failed_rows' => 'integer',
            'error_summary' => 'array',
            'options' => 'array',
        ];
    }

    public function operator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'operator_user_id');
    }

    public function rows(): HasMany
    {
        return $this->hasMany(DberpDataImportBatchRow::class, 'batch_id');
    }
}
