<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Models;

use App\Business\DataImport\Enums\DataImportBatchStatus;
use App\Casts\AsDataImportImportType;
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' => AsDataImportImportType::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');
    }
}
