<?php

namespace App\Imports;

use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
use Illuminate\Support\Facades\Log;
use App\Jobs\ImportArtistChunkJob;
use App\Jobs\ImportSongChunkJob;

class DataImport implements ToCollection, WithHeadingRow, WithChunkReading
{
    protected int $con=0;
    protected string $modelName;
    public function __construct(string $modelName)
    {
        HeadingRowFormatter::default('none');
        $this->modelName= $modelName;
    }
    public function collection(Collection $rows)
    {
        
        Log::warning('匯入啟動', [
            'model' => $this->modelName,
            'rows_id' =>++$this->con,
            'rows_count' => $rows->count()
        ]);
        if($this->modelName=='Song'){
            ImportSongChunkJob::dispatch($rows,$this->con);
        }else if($this->modelName=='Artist'){
            ImportArtistChunkJob::dispatch($rows,$this->con);
        }else{
            Log::warning('未知的 modelName', ['model' => $this->modelName]);
        }
    }
    public function chunkSize(): int
    {
        return 1000;
    }

    public function headingRow(): int
    {
        return 1;
    }
}