<?php

namespace App\Imports;

use Carbon\Carbon;
use App\Models\DMS;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsErrors;
use Maatwebsite\Excel\Concerns\SkipsFailures;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithProgressBar;
use DB;
use Illuminate\Support\Arr;


class ImportDMS implements ToModel , WithStartRow , SkipsOnError, SkipsOnFailure ,WithProgressBar
{
    use Importable ,SkipsErrors ,SkipsFailures ;
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function startRow(): int
    {
        return 1;
    }
    
    public function model(array $row)
    {
        $exist_data = DMS::select('id')->where(['vin_number' => $row[2]])->first();
        
        $data = [];
        if($exist_data){
            session(['error' => 'Some DMS is already available on records!']);
            return null;
        }else{
            if (\DateTime::createFromFormat('d-m-Y', $row[0])) {
                $formattedDate = Carbon::createFromFormat('d-m-Y', $row[0])->format('Y-m-d');
                $formattedGatepassDate = Carbon::createFromFormat('d-m-Y', $row[5])->format('Y-m-d');
            } elseif (\DateTime::createFromFormat('d/m/Y', $row[0])) {
                $formattedDate = Carbon::createFromFormat('d/m/Y', $row[0])->format('Y-m-d');
                $formattedGatepassDate = Carbon::createFromFormat('d/m/Y', $row[5])->format('Y-m-d');
            } elseif (is_numeric($row[0])) {
                // Handle Excel numeric date (e.g., 44560)
                $formattedDate = Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row[0]))->format('Y-m-d');
                $formattedGatepassDate = Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row[5]))->format('Y-m-d');
            }
            
            $basicAmount = isset($row[3]) ? (float) preg_replace('/[^\d.]/', '', $row[3]) : 0;
            $gstAmount = isset($row[4]) ? (float) preg_replace('/[^\d.]/', '', $row[4]) : 0;
            
            $data = [
                'date' => isset($formattedDate) ? $formattedDate : null,
                'dms_invoice_no' => isset($row[1]) ? $row[1] : null,
                'vin_number'          => isset($row[2]) ? $row[2] : null,
                'basic_amount' => $basicAmount,
                'gst_amount' => $gstAmount,
                'total_amount' => ($basicAmount + $gstAmount),
                'getpass_closing_date'    => isset($formattedGatepassDate) ? $formattedGatepassDate : null,
                'scheme'    => isset($row[6]) ? $row[6] : null,
                'remark'    => isset($row[7]) ? $row[7] : null,
                'created_at' => date('Y-m-d H:i:s'),
                'created_by' => auth()->user()->id,
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        }

        if (empty($data['vin_number']) || empty($data['date'])) {
            return null;
        }
    
        return DMS::create($data);
    }
}
