<?php

namespace App\Modules\Inventory\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Inventory\Models\Category;
use App\Modules\Inventory\Models\Warehouse;
use App\Modules\Inventory\Services\InventoryReportService;
use App\Modules\Inventory\Exports\InventoryReportExport;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Maatwebsite\Excel\Facades\Excel;


class InventoryReportController extends Controller
{
    protected $reportService;

    public function __construct(InventoryReportService $reportService)
    {
        $this->reportService = $reportService;
    }

    public function index(Request $request)
    {
        $filters = $request->only([
            'date_from', 'date_to', 'warehouse_id', 'category_id', 'search', 'per_page',
            'sort_by', 'sort_order'
        ]);

        if (!isset($filters['date_from'])) {
            $filters['date_from'] = date('Y-m-d');
        }
        if (!isset($filters['date_to'])) {
            $filters['date_to'] = date('Y-m-d');
        }

        $reportData = $this->reportService->getReportData($filters, $request->input('per_page', 10));
        $summary = $this->reportService->getSummary($filters);

        return Inertia::render('Inventory/Report/Index', [
            'reportData' => $reportData,
            'summary' => $summary,
            'warehouses' => Warehouse::select('id', 'name')->get(),
            'categories' => Category::select('id', 'name')->get(),
            'filters' => $filters,
        ]);
    }

    public function export(Request $request)
    {
        $filters = $request->only([
            'period', 'date_from', 'date_to', 'warehouse_id', 'category_id', 'search'
        ]);

        return Excel::download(new InventoryReportExport($this->reportService, $filters), 'inventory_report_' . date('YmdHis') . '.xlsx');
    }

    public function show(Request $request, $productId)
    {
        // 明細頁面自身使用的篩選條件
        $filters = $request->only([
            'date_from', 'date_to', 'warehouse_id'
        ]);

        // 報表頁面的完整篩選狀態（用於返回時恢復）
        $reportFilters = $request->only([
            'date_from', 'date_to', 'warehouse_id',
            'category_id', 'search', 'per_page'
        ]);
        // 將傳入的 report_page 轉回 page 以便 Link 元件正確生成回報表頁的連結
        if ($request->has('report_page')) {
            $reportFilters['page'] = $request->input('report_page');
        }

        // 取得商品資訊 (用於顯示標題，含基本單位)
        $product = \App\Modules\Inventory\Models\Product::with('baseUnit')->findOrFail($productId);
        
        $transactions = $this->reportService->getProductDetails($productId, $filters, 20);

        return Inertia::render('Inventory/Report/Show', [
            'product' => [
                'id' => $product->id,
                'code' => $product->code,
                'name' => $product->name,
                'unit_name' => $product->baseUnit?->name ?? '-',
            ],
            'transactions' => $transactions,
            'filters' => $filters,
            'reportFilters' => $reportFilters,
            'warehouses' => Warehouse::select('id', 'name')->get(),
        ]);
    }
}
