<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\SupplierAccount;
use App\Models\Company;
use PDF;

class SupplierListingReportController extends Controller
{
    // Display initial form
    public function index()
    {
        $suppliers = SupplierAccount::with(['category', 'ageAnalysis', 'area'])
            ->orderBy('supplier_code')
            ->get();
        
        // Initialize empty values for the form
        $fromSupplierId = null;
        $toSupplierId = null;
        
        return view('reports.supplier-listing', compact('suppliers', 'fromSupplierId', 'toSupplierId'));
    }

    // Generate supplier listing report
    public function generate(Request $request)
    {
        $request->validate([
            'from_supplier' => 'required',
        ]);

        $fromSupplierId = $request->from_supplier;
        $toSupplierId = $request->to_supplier;

        // Get all suppliers for dropdowns (with relationships)
        $allSuppliers = SupplierAccount::with(['category', 'ageAnalysis', 'area'])
            ->orderBy('supplier_code')
            ->get();

        // Handle "All Suppliers" selection
        if ($fromSupplierId === 'all') {
            $suppliers = SupplierAccount::with(['category', 'ageAnalysis', 'area'])
                ->orderBy('supplier_code')
                ->get();
            
            return view('reports.supplier-listing', compact(
                'allSuppliers',
                'suppliers',
                'fromSupplierId'
            ));
        }

        // Validate to_supplier when from_supplier is not "all"
        $request->validate([
            'to_supplier' => 'required',
        ]);

        // Get the supplier codes for range filtering
        $fromSupplier = SupplierAccount::find($fromSupplierId);
        $toSupplier = SupplierAccount::find($toSupplierId);

        // Get suppliers in the selected range with relationships
        $suppliers = SupplierAccount::with(['category', 'ageAnalysis', 'area'])
            ->whereBetween('supplier_code', [$fromSupplier->supplier_code, $toSupplier->supplier_code])
            ->orderBy('supplier_code')
            ->get();

        return view('reports.supplier-listing', compact(
            'allSuppliers',
            'suppliers',
            'fromSupplierId',
            'toSupplierId'
        ));
    }

    public function downloadPdf(Request $request)
    {
        $fromSupplierId = $request->from_supplier;
        $toSupplierId = $request->to_supplier;

        // Handle "All Suppliers" selection
        if ($fromSupplierId === 'all') {
            $suppliers = SupplierAccount::with(['category', 'ageAnalysis', 'area'])
                ->orderBy('supplier_code')
                ->get();
            $fromSupplier = (object)['supplier_code' => 'ALL'];
            $toSupplier = (object)['supplier_code' => 'ALL'];
        } else {
            // Get the supplier codes for range filtering
            $fromSupplier = SupplierAccount::find($fromSupplierId);
            $toSupplier = SupplierAccount::find($toSupplierId);

            // Get suppliers in the selected range with relationships
            $suppliers = SupplierAccount::with(['category', 'ageAnalysis', 'area'])
                ->whereBetween('supplier_code', [$fromSupplier->supplier_code, $toSupplier->supplier_code])
                ->orderBy('supplier_code')
                ->get();
        }

        // Get company info
        $company = Company::first();

        $pdf = PDF::loadView('reports.supplier-listing-pdf', compact(
            'suppliers',
            'company',
            'fromSupplier',
            'toSupplier'
        ));

        return $pdf->download('supplier-listing-'.date('Y-m-d').'.pdf');
    }
}