<?php

namespace App\Exports;

use App\Models\Inventory;
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 ExportInventoryReport 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('inventory as i')
        ->select(
            'cm.name as model_name',
            'cv.name as variant',
            'i.status',
            'i.vin_number',
            'b.name as branch',
            'Team_master.team_name as team_leader',
            DB::raw("CONCAT(users.first_name, ' ', users.last_name) AS kec_name"),
            'i.ex_showroom_price',
            'i.purchase_price',
            'i.kin_margin',
            'fuel_type.title as fuel_type',
            DB::raw('(CASE WHEN i.status = "allocated" THEN obf.customer_name END) AS customer_name'),
            'i.aging_days',
            'i.inventory_type'
        )
        ->leftJoin('branches as b', 'b.id', '=', 'i.branch_id')
        ->leftJoin('products', 'i.name', '=', 'products.id')
        ->leftJoin('car_model as cm', 'cm.id', '=', 'products.car_model')
        ->leftJoin('car_varient as cv', 'cv.id', '=', 'products.veriant')
        ->leftJoin('fule_type as fuel_type', 'fuel_type.id', '=', 'i.fuel_type')
        ->leftJoin('obf', 'obf.product_id', '=', 'i.name')
        ->leftJoin('users', 'users.id', '=', 'obf.sales_person_id')
        ->leftJoin('Team_master', 'Team_master.id', '=', 'users.team_name')
        ->where('i.status', '!=', "allocatedDealer")
        ->where('i.status', '!=', 'deleted')
        ->groupBy(
            'cm.name',
            'cv.name',
            'i.status',
            'i.vin_number',
            'b.name',
            'Team_master.team_name',
            'users.first_name',
            'users.last_name',
            'i.ex_showroom_price',
            'i.purchase_price',
            'i.kin_margin',
            'fuel_type.title',
            'obf.customer_name',
            'i.aging_days',
            'i.inventory_type'
        )
        ->orderBy('i.id', 'desc')
        ->get();
 

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

        return $data;
    }

    public function headings(): array
    {
        return [ "Model Name",
        "Variant",
        "Status",
        "VIN Number",
        "Branch",
        "Team Leader",
        "KEC Name",
        "Ex Showroom Price",
        "Purchase Price",
        "KIN Margin",
        "Fuel Type",
        "Customer Name",
        "Aging Days",
        "Inventory Type"];
    }

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