<?php

namespace App\Exports;

use App\Models\Product;
use App\Models\OBF;
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 ExportMunicipleTaxReport implements FromCollection, WithHeadings, WithStyles, ShouldAutoSize
{
    // use Exportable;

    protected $slug;
    protected $vin;

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

    public function collection()
    {
        $data = OBF::query()
                ->select(
                    'obf.id',
                    'car_model.name as model_name',
                    'car_varient.name as variant_name',
                    DB::raw("CONCAT(UCASE(LEFT(REPLACE(obf.status, '_', ' '), 1)), 
                     LOWER(SUBSTRING(REPLACE(obf.status, '_', ' '), 2))) AS status"),
                    'inventory.vin_number',
                    'exteriorcolor.name as exterior_color',
                    'interiorcolor.name as interior_color',
                    DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),
                    'Team_master.team_name',
                    'obf_municiple_tax.rmc_receipt_no',
                    'obf_municiple_tax.tax_type',
                    'obf_municiple_tax.remarks',
                    'obf_municiple_tax.municiple_date',
                    'obf_municiple_tax.status as municiple_status'
                )
                ->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('inventory', 'inventory.name', '=', 'products.id')
                ->leftJoin('exteriorcolor', 'exteriorcolor.id', '=', 'products.exterior_color')
                ->leftJoin('interiorcolor', 'interiorcolor.id', '=', 'products.interior_color')
                ->leftJoin('users', 'users.id', '=', 'obf.sales_person_id')
                ->leftJoin('Team_master', 'Team_master.id', '=', 'users.team_name')
                ->leftJoin('obf_municiple_tax', 'obf_municiple_tax.obf_id', '=', 'obf.id')
                ->where('obf_municiple_tax.status', 'municipal_taxx_comlpleted')
                ->when($this->vin, function ($query) {
                    return $query->where('inventory.vin_number', '=', $this->vin);
                })
                ->groupBy('obf.id')
                ->get();

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

        return $data;
    }

    public function headings(): array
    {
        return ["No", "Model Name", "Variant", "Status", "VIN Number", "Exterior Color", "Interior Color", "Team Leader", "KEC Name","RMC Receipt No","Tax Type","Remarks","Municiple Status"];
    }

    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;
    }
}
