<?php

namespace App\Http\Controllers;

use App\Models\DocumentHeader;
use App\Models\CompanySettings;
use Illuminate\Http\Request;
use PDF;

class InvoiceReportController extends Controller
{
    public function index()
    {
        return view('reports.invoice-summary');
    }

    public function generate(Request $request)
    {
        $request->validate([
            'report_type' => 'required|in:summary,detailed',
            'from_date' => 'required|date',
            'to_date' => 'required|date|after_or_equal:from_date',
        ]);

        $reportType = $request->report_type;
        $fromDate = $request->from_date;
        $toDate = $request->to_date;
        $company = CompanySettings::first();

        // Get invoices within date range
        $invoices = DocumentHeader::where('type', 'invoice')
            ->whereBetween('document_date', [$fromDate, $toDate])
            ->with('customer')
            ->get();

        if ($reportType === 'summary') {
            return view('reports.invoice-summary', compact('reportType', 'fromDate', 'toDate', 'invoices', 'company'));
        } else {
            // Group invoices by customer for detailed report
            $customerInvoices = [];
            foreach ($invoices as $invoice) {
                $customerCode = $invoice->customer->customer_code;
                if (!isset($customerInvoices[$customerCode])) {
                    $customerInvoices[$customerCode] = [
                        'customer' => $invoice->customer,
                        'invoices' => []
                    ];
                }
                $customerInvoices[$customerCode]['invoices'][] = $invoice;
            }

            return view('reports.invoice-summary', compact('reportType', 'fromDate', 'toDate', 'customerInvoices', 'invoices', 'company'));
        }
    }

    public function downloadPdf(Request $request)
    {
        $request->validate([
            'report_type' => 'required|in:summary,detailed',
            'from_date' => 'required|date',
            'to_date' => 'required|date',
        ]);

        $reportType = $request->report_type;
        $fromDate = $request->from_date;
        $toDate = $request->to_date;
        $company = CompanySettings::first();

        // Get invoices within date range
        $invoices = DocumentHeader::where('type', 'invoice')
            ->whereBetween('document_date', [$fromDate, $toDate])
            ->with('customer')
            ->get();

        if ($reportType === 'summary') {
            $pdf = PDF::loadView('reports.invoice-summary-pdf', compact('reportType', 'fromDate', 'toDate', 'invoices', 'company'));
        } else {
            // Group invoices by customer for detailed report
            $customerInvoices = [];
            foreach ($invoices as $invoice) {
                $customerCode = $invoice->customer->customer_code;
                if (!isset($customerInvoices[$customerCode])) {
                    $customerInvoices[$customerCode] = [
                        'customer' => $invoice->customer,
                        'invoices' => []
                    ];
                }
                $customerInvoices[$customerCode]['invoices'][] = $invoice;
            }

            $pdf = PDF::loadView('reports.invoice-detail-pdf', compact('reportType', 'fromDate', 'toDate', 'customerInvoices', 'invoices', 'company'));
        }

        return $pdf->download('invoice-summary-report-'.now()->format('Y-m-d').'.pdf');
    }
}