<?php

namespace App\Exports;

use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Validators\Failure;

class ExportRTOReport implements FromCollection, WithHeadings, WithStyles, ShouldAutoSize
{
    protected $slug;
    protected $vin;

    public function __construct($slug, $vin = null) {
        $this->slug = $slug;
        $this->vin = $vin;
    }

    public function collection()
    {
        $data = DB::table('obf')
                ->join('obf_rto', 'obf.id', '=', 'obf_rto.obf_id')
                ->leftJoin('rto_process_dms', 'obf.id', '=', 'rto_process_dms.obf_id')
                ->leftJoin('products', 'obf.product_id', '=', 'products.id')
                ->leftJoin('car_varient', 'car_varient.id', '=', 'products.veriant')
                ->leftJoin('car_model', 'car_model.id', '=', 'products.car_model')
                ->leftJoin('users', 'users.id', '=', 'obf.sales_person_id')
                ->leftJoin('Team_master', 'Team_master.id', '=', 'users.team_name')
                ->select([
                    'obf.id as No',
                    'car_model.name AS Model', 
                    'car_varient.name AS Variant', 
                    'obf.status AS Status', 
                    'Team_master.team_name AS Team_Leader',
                    DB::raw("CONCAT(users.first_name, ' ', users.last_name) AS KEC_Name"),
                    'obf.rto_status AS RTO_Status',
                    DB::raw("REPLACE(obf_rto.rto_type, '_', ' ') AS RTO_Type"),
                    'obf.number_plate AS Number_Plate',
                    'obf_rto.registration_date AS Registration_Number',
                    'obf_rto.application_number AS Application_Number',
                    'obf_rto.temporary_number AS Temporary_Number',
                    'obf_rto.final_amount AS Final_Amount',
                    'obf_rto.vehicle_number AS Vehicle_Number',
                    'rto_process_dms.rto_invoice AS RTO_Invoice',
                    'rto_process_dms.rto_invoice_date AS RTO_Invoice_Date',
                    'rto_process_dms.dms_invoice_no AS DMS_Invoice_No',
                    'rto_process_dms.dms_invoice_date AS DMS_Invoice_Date',
                    'rto_process_dms.dms_gatepass_date AS DMS_Gate_Pass',
                    'rto_process_dms.dms_location AS DMS_Location',
                    'rto_process_dms.dms_rto_create_date AS DMS_RTO_Create_Date',
                    'rto_process_dms.dms_rto_certificate AS RTO_Certificate',
                    'rto_process_dms.status AS RTO_Process_Status'
                ])
                ->when($this->vin, function ($query) {
                    return $query->where('obf.vin', 'LIKE', "%{$this->vin}%");
                })
                ->get();

        if ($data->isEmpty()) {
            throw new \ErrorException('No Data Found!');
        }

        return $data;
    }

    public function headings(): array
    {
        return ["No", "Model", "Variant", "Status", "Team Leader", "KEC Name","RTO Status","RTO Type","Number Plate","Registration Number","Application Number","Temporary Number","Final Amount","Vehicle Number","RTO Invoice","RTO Invoice Date","DMS Invoice No","DMS Invoice Date","DMS Gate Pass","DMS Location","DMS RTO Create Date","RTO Certificate","RTO Process Status"];
    }

    public function styles(Worksheet $sheet)
    {
        return [
            1 => ['font' => ['bold' => true]], // Make the first row bold
        ];
    }

    public function onFailure(Failure ...$failures)
    {
        $exception = ValidationException::withMessages(collect($failures)->map->toArray()->all());
        throw $exception;
    }
}
