<?php

namespace App\Exports;

use DB;
use App\Models\Branch;
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;

class ExportCarDetails implements FromCollection, WithHeadings,WithStyles ,ShouldAutoSize{
    protected $ids;
    function __construct($ids) {
        $this->ids = $ids;
    }

    // public function collection(){
    //     $model = new Branch();
    //     $data = DB::table('inventory')
    //     ->join('products', 'inventory.name', '=', 'products.id')
    //     ->join('branches', 'branches.id', '=', 'inventory.branch_id')
    //     ->leftJoin('car_model', 'car_model.id', '=', 'products.car_model')
    //     ->leftJoin('car_varient', 'car_varient.id', '=', 'products.veriant')
    //     ->leftJoin('exteriorcolor', 'exteriorcolor.id', '=', 'products.exterior_color')
    //     ->leftJoin('interiorcolor', 'interiorcolor.id', '=', 'products.interior_color')
    //     ->leftJoin('fule_type', 'fule_type.id', '=', 'inventory.fuel_type')
    //     ->leftJoin('obf', 'obf.inventory_id', '=', 'inventory.id')
    //     ->leftJoin('dealer_management', 'dealer_management.id', '=', 'inventory.delear_id')
    //     ->whereIn('inventory.id', $this->ids)
    //     ->select(
    //         'branches.name as branch_name',
    //         'car_model.name as car_name',
    //         'car_varient.name as varient_name',
    //         'fule_type.title as fuel_type_name',
    //         'inventory.vin_number',
    //         'exteriorcolor.name as exterior_color',
    //         'interiorcolor.name as interior_color',
    //         'inventory.aging_days',
    //         'inventory.status',
    //         DB::raw("COALESCE(obf.customer_name, dealer_management.dealer_name) as customer_name")
    //     )
    //     ->groupBy('inventory.id')
    //     ->get();
    
    //     if($data == null){
    //         return throw new \ErrorException('No data found');
    //     }else{
    //         return $data;
    //     }
    // }
    
    public function collection()
    {
        $ids = $this->ids['ids'] ?? [];
        $transitIds = $this->ids['transit_ids'] ?? [];
    
        // Prepare collections for both inventory and transit
        $inventoryData = collect();
        $transitData = collect();
    
        if (!empty($ids)) {
            $inventoryData = DB::table('inventory')
                ->join('products', 'inventory.name', '=', 'products.id')
                ->join('branches', 'branches.id', '=', 'inventory.branch_id')
                ->leftJoin('car_model', 'car_model.id', '=', 'products.car_model')
                ->leftJoin('car_varient', 'car_varient.id', '=', 'products.veriant')
                ->leftJoin('exteriorcolor', 'exteriorcolor.id', '=', 'products.exterior_color')
                ->leftJoin('interiorcolor', 'interiorcolor.id', '=', 'products.interior_color')
                ->leftJoin('fule_type', 'fule_type.id', '=', 'inventory.fuel_type')
                ->leftJoin('obf', 'obf.inventory_id', '=', 'inventory.id')
                ->leftJoin('dealer_management', 'dealer_management.id', '=', 'inventory.delear_id')
                ->whereIn('inventory.id', $ids)
                ->select(
                    'branches.name as branch_name',
                    'car_model.name as car_name',
                    'car_varient.name as varient_name',
                    'fule_type.title as fuel_type_name',
                    'inventory.vin_number',
                    'exteriorcolor.name as exterior_color',
                    'interiorcolor.name as interior_color',
                    'inventory.aging_days',
                    'inventory.status',
                    DB::raw("COALESCE(obf.customer_name, dealer_management.dealer_name) as customer_name")
                )
                ->groupBy('inventory.id')
                ->get();
        }
    
        if (!empty($transitIds)) {
            $transitData = DB::table('transit_inventory')
                ->join('products', 'transit_inventory.name', '=', 'products.id')
                ->join('branches', 'branches.id', '=', 'transit_inventory.branch_id')
                ->leftJoin('car_model', 'car_model.id', '=', 'products.car_model')
                ->leftJoin('car_varient', 'car_varient.id', '=', 'products.veriant')
                ->leftJoin('exteriorcolor', 'exteriorcolor.id', '=', 'products.exterior_color')
                ->leftJoin('interiorcolor', 'interiorcolor.id', '=', 'products.interior_color')
                ->leftJoin('fule_type', 'fule_type.id', '=', 'transit_inventory.fuel_type')
                ->whereIn('transit_inventory.id', $transitIds)
                ->select(
                    'branches.name as branch_name',
                    'car_model.name as car_name',
                    'car_varient.name as varient_name',
                    'fule_type.title as fuel_type_name',
                    'transit_inventory.vin_number',
                    'exteriorcolor.name as exterior_color',
                    'interiorcolor.name as interior_color',
                    'transit_inventory.aging_days',
                    'transit_inventory.status'
                )
                ->groupBy('transit_inventory.id')
                ->get();
        }
    
        // Combine both collections
        $combinedData = $inventoryData->merge($transitData);
    
        if ($combinedData->isEmpty()) {
            throw new \ErrorException('No data found');
        }
    
        return $combinedData;
    }


    public function headings() :array{
        return ['Location','Model','Variant','Fuel Type','VIN No.','Exterior Color','Interior Color','Aging Days','Status','Customer Name'];
    }

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