<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\CustomerAccount;
use App\Models\CompanySettings; // Changed from Company to CompanySettings
use PDF;

class CustomerListingReportController extends Controller
{
    // Display initial form
    public function index()
    {
        $customers = CustomerAccount::with(['category', 'salesRep', 'ageAnalysis', 'area', 'foreignCurrency'])
            ->orderBy('customer_code')
            ->get();
        
        // Initialize empty values for the form
        $fromCustomerId = null;
        $toCustomerId = null;
        
        return view('reports.customer-listing', compact('customers', 'fromCustomerId', 'toCustomerId'));
    }

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

        $fromCustomerId = $request->from_customer;
        $toCustomerId = $request->to_customer;

        // Get all customers for dropdowns (with relationships)
        $allCustomers = CustomerAccount::with(['category', 'salesRep', 'ageAnalysis', 'area', 'foreignCurrency'])
            ->orderBy('customer_code')
            ->get();

        // Handle "All Customers" selection
        if ($fromCustomerId === 'all') {
            $customers = CustomerAccount::with(['category', 'salesRep', 'ageAnalysis', 'area', 'foreignCurrency'])
                ->orderBy('customer_code')
                ->get();
            
            return view('reports.customer-listing', compact(
                'allCustomers',
                'customers',
                'fromCustomerId'
            ));
        }

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

        // Get the customer codes for range filtering
        $fromCustomer = CustomerAccount::find($fromCustomerId);
        $toCustomer = CustomerAccount::find($toCustomerId);

        // Get customers in the selected range with relationships
        $customers = CustomerAccount::with(['category', 'salesRep', 'ageAnalysis', 'area', 'foreignCurrency'])
            ->whereBetween('customer_code', [$fromCustomer->customer_code, $toCustomer->customer_code])
            ->orderBy('customer_code')
            ->get();

        return view('reports.customer-listing', compact(
            'allCustomers',
            'customers',
            'fromCustomerId',
            'toCustomerId'
        ));
    }

    public function downloadPdf(Request $request)
    {
        $fromCustomerId = $request->from_customer;
        $toCustomerId = $request->to_customer;

        // Handle "All Customers" selection
        if ($fromCustomerId === 'all') {
            $customers = CustomerAccount::with(['category', 'salesRep', 'ageAnalysis', 'area', 'foreignCurrency'])
                ->orderBy('customer_code')
                ->get();
            $fromCustomer = (object)['customer_code' => 'ALL'];
            $toCustomer = (object)['customer_code' => 'ALL'];
        } else {
            // Get the customer codes for range filtering
            $fromCustomer = CustomerAccount::find($fromCustomerId);
            $toCustomer = CustomerAccount::find($toCustomerId);

            // Get customers in the selected range with relationships
            $customers = CustomerAccount::with(['category', 'salesRep', 'ageAnalysis', 'area', 'foreignCurrency'])
                ->whereBetween('customer_code', [$fromCustomer->customer_code, $toCustomer->customer_code])
                ->orderBy('customer_code')
                ->get();
        }

        // Get company info - Changed from Company to CompanySettings
        $company = CompanySettings::first();

        $pdf = PDF::loadView('reports.customer-listing-pdf', compact(
            'customers',
            'company',
            'fromCustomer',
            'toCustomer'
        ));

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