<?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 ExportTransferReport 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('transfer as t')
                ->leftJoin('branches as tb', 'tb.id', '=', 't.to_branch')
                ->leftJoin('branches as fb', 'fb.id', '=', 't.from_branch')
                ->leftJoin('inventory as i', 'i.id', '=', 't.inventory_id')
                ->leftJoin('products as p', 'p.id', '=', 'i.name')
                ->leftJoin('car_model as cm', 'cm.id', '=', 'p.car_model')
                ->leftJoin('car_varient as cv', 'cv.id', '=', 'p.veriant')
                ->select([
                    't.id AS No',
                    'fb.name AS From_Branch',
                    'tb.name AS To_Branch',
                    'cm.name AS Car_Model',
                    'cv.name AS Car_Variant',
                    'i.vin_number AS VIN_Number',
                    't.driver_name AS Driver_Name',
                    't.driver_lic AS Driver_License_Number',
                    't.driver_pan AS Driver_Pan_Number',
                    't.remark AS Transfer_Remark',
                    DB::raw("CASE WHEN t.toolkit = 1 THEN 'Yes' ELSE 'No' END AS ToolKit"),
                    DB::raw("CASE WHEN t.medikit = 1 THEN 'Yes' ELSE 'No' END AS HealthKit"),
                    DB::raw("CASE WHEN t.spare_bulb = 1 THEN 'Yes' ELSE 'No' END AS SpareBulb"),
                    DB::raw("CASE WHEN t.hazard_trinagle = 1 THEN 'Yes' ELSE 'No' END AS HazardTriangle"),
                    DB::raw("CASE WHEN t.good_condtiton = 1 THEN 'Yes' ELSE 'No' END AS Received_Good_Condition"),
                    't.created_at AS Transfer_Date'
                ])
                ->when($this->vin, function ($query) {
                    return $query->where('i.vin_number', 'like', "%{$this->vin}%"); 
                })
                ->get();

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

        return $data;
    }

    public function headings(): array
    {
        return ["No", "From Branch", "To Branch", "Car Model", "Car Variant", "VIN Number", "Driver Name", "Driver License Number", "Driver Pan Number", "Transfer Remark", "ToolKit", "HealthKit", "SpareBulb", "HazardTriangle", "Received Vehicle in Good Condition", "Transfer Date"];
    }

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

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