<?php

namespace App\Http\Controllers;

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

class SupplierInvoiceReportController extends Controller
{
    public function index()
    {
        // Set default dates to current date
        $currentDate = now()->format('Y-m-d');
        
        return view('reports.supplier-invoice-summary', compact('currentDate'));
    }

    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 supplier invoices (sales_invoice type) within date range
        $invoices = SupplierDocumentHeader::where('type', 'sales_invoice')
            ->whereBetween('document_date', [$fromDate, $toDate])
            ->with('supplier')
            ->get();

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

            return view('reports.supplier-invoice-summary', compact('reportType', 'fromDate', 'toDate', 'supplierInvoices', '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 supplier invoices (sales_invoice type) within date range
        $invoices = SupplierDocumentHeader::where('type', 'sales_invoice')
            ->whereBetween('document_date', [$fromDate, $toDate])
            ->with('supplier')
            ->get();

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

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

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