<?php

namespace App\Modules\Inventory\Controllers;

use App\Http\Controllers\Controller;

use App\Modules\Inventory\Models\Product;
use App\Modules\Inventory\Models\Unit;
use App\Modules\Inventory\Models\Category;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
use Maatwebsite\Excel\Facades\Excel;
use App\Modules\Inventory\Exports\ProductTemplateExport;
use App\Modules\Inventory\Imports\ProductImport;

class ProductController extends Controller
{
    /**
     * 顯示資源列表。
     */
    public function index(Request $request): Response
    {
        $query = Product::with(['category', 'baseUnit', 'largeUnit', 'purchaseUnit']);

        if ($request->filled('search')) {
            $search = $request->search;
            $query->where(function ($q) use ($search) {
                $q->where('name', 'like', "%{$search}%")
                  ->orWhere('code', 'like', "%{$search}%")
                  ->orWhere('barcode', 'like', "%{$search}%")
                  ->orWhere('brand', 'like', "%{$search}%");
            });
        }

        if ($request->filled('category_id') && $request->category_id !== 'all') {
            $query->where('category_id', $request->category_id);
        }

        $perPage = $request->input('per_page', 10);
        if (!in_array($perPage, [10, 20, 50, 100])) {
            $perPage = 10;
        }

        $sortField = $request->input('sort_field', 'id');
        $sortDirection = $request->input('sort_direction', 'desc');
        
        // 定義允許的排序欄位以防止 SQL 注入
        $allowedSorts = ['id', 'code', 'name', 'category_id', 'base_unit_id', 'conversion_rate'];
        if (!in_array($sortField, $allowedSorts)) {
            $sortField = 'id';
        }
        if (!in_array(strtolower($sortDirection), ['asc', 'desc'])) {
            $sortDirection = 'desc';
        }

        // 如果需要，分別處理關聯排序（分類名稱），或簡單的 join
        if ($sortField === 'category_id') {
            // 加入分類以便按名稱排序？還是僅按 ID？
            // 簡單方法：目前按 ID 排序，如果使用者想要按名稱排序則 join。
            // 先假設標準欄位排序。
            $query->orderBy('category_id', $sortDirection);
        } else {
             $query->orderBy($sortField, $sortDirection);
        }

        $products = $query->paginate($perPage)->withQueryString();

        $products->getCollection()->transform(function ($product) {
            return (object) [
                'id' => (string) $product->id,
                'code' => $product->code,
                'barcode' => $product->barcode,
                'name' => $product->name,
                'categoryId' => $product->category_id,
                'category' => $product->category ? (object) [
                    'id' => $product->category->id,
                    'name' => $product->category->name,
                ] : null,
                'brand' => $product->brand,
                'specification' => $product->specification,
                'baseUnitId' => $product->base_unit_id,
                'baseUnit' => $product->baseUnit ? (object) [
                    'id' => $product->baseUnit->id,
                    'name' => $product->baseUnit->name,
                ] : null,
                'largeUnitId' => $product->large_unit_id,
                'largeUnit' => $product->largeUnit ? (object) [
                    'id' => $product->largeUnit->id,
                    'name' => $product->largeUnit->name,
                ] : null,
                'purchaseUnitId' => $product->purchase_unit_id,
                'purchaseUnit' => $product->purchaseUnit ? (object) [
                    'id' => $product->purchaseUnit->id,
                    'name' => $product->purchaseUnit->name,
                ] : null,
                'conversionRate' => (float) $product->conversion_rate,
                'location' => $product->location,
                'cost_price' => (float) $product->cost_price,
                'price' => (float) $product->price,
                'member_price' => (float) $product->member_price,
                'wholesale_price' => (float) $product->wholesale_price,
                'is_active' => (bool) $product->is_active,
            ];
        });

        $categories = Category::where('is_active', true)->get();

        return Inertia::render('Product/Index', [
            'products' => $products,
            'categories' => Category::where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
            'units' => Unit::all()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
            'filters' => $request->only(['search', 'category_id', 'per_page', 'sort_field', 'sort_direction']),
        ]);
    }

    /**
     * 顯示指定的資源。
     */
    public function show(Product $product): Response
    {
        return Inertia::render('Product/Show', [
            'product' => (object) [
                'id' => (string) $product->id,
                'code' => $product->code,
                'barcode' => $product->barcode,
                'name' => $product->name,
                'categoryId' => $product->category_id,
                'category' => $product->category ? (object) [
                    'id' => $product->category->id,
                    'name' => $product->category->name,
                ] : null,
                'brand' => $product->brand,
                'specification' => $product->specification,
                'baseUnitId' => $product->base_unit_id,
                'baseUnit' => $product->baseUnit ? (object) [
                    'id' => $product->baseUnit->id,
                    'name' => $product->baseUnit->name,
                ] : null,
                'largeUnitId' => $product->large_unit_id,
                'largeUnit' => $product->largeUnit ? (object) [
                    'id' => $product->largeUnit->id,
                    'name' => $product->largeUnit->name,
                ] : null,
                'purchaseUnitId' => $product->purchase_unit_id,
                'purchaseUnit' => $product->purchaseUnit ? (object) [
                    'id' => $product->purchaseUnit->id,
                    'name' => $product->purchaseUnit->name,
                ] : null,
                'conversionRate' => (float) $product->conversion_rate,
                'location' => $product->location,
                'cost_price' => (float) $product->cost_price,
                'price' => (float) $product->price,
                'member_price' => (float) $product->member_price,
                'wholesale_price' => (float) $product->wholesale_price,
                'is_active' => (bool) $product->is_active,
            ]
        ]);
    }

    /**
     * 顯示建立表單。
     */
    public function create(): Response
    {
        return Inertia::render('Product/Create', [
            'categories' => Category::where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
            'units' => Unit::all()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
        ]);
    }

    /**
     * 將新建立的資源儲存到儲存體中。
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'code' => 'nullable|unique:products,code',
            'barcode' => 'nullable|unique:products,barcode',
            'name' => 'required|string|max:255',
            'category_id' => 'required|exists:categories,id',
            'brand' => 'nullable|string|max:255',
            'specification' => 'nullable|string',
            'base_unit_id' => 'required|exists:units,id',
            'large_unit_id' => 'nullable|exists:units,id',
            'purchase_unit_id' => 'nullable|exists:units,id',
            'conversion_rate' => 'nullable|numeric|min:0',
            'location' => 'nullable|string|max:255',
            'cost_price' => 'nullable|numeric|min:0',
            'price' => 'nullable|numeric|min:0',
            'member_price' => 'nullable|numeric|min:0',
            'wholesale_price' => 'nullable|numeric|min:0',
            'is_active' => 'boolean',
        ]);

        if (empty($validated['code'])) {
            $validated['code'] = $this->generateRandomCode();
        }

        if (empty($validated['barcode'])) {
            $validated['barcode'] = $this->generateRandomBarcode();
        }

        $product = Product::create($validated);

        return redirect()->route('products.index')->with('success', '商品已建立');
    }

    /**
     * 顯示編輯表單。
     */
    public function edit(Product $product): Response
    {
        return Inertia::render('Product/Edit', [
            'product' => (object) [
                'id' => (string) $product->id,
                'code' => $product->code,
                'barcode' => $product->barcode,
                'name' => $product->name,
                'categoryId' => $product->category_id,
                'brand' => $product->brand,
                'specification' => $product->specification,
                'baseUnitId' => $product->base_unit_id,
                'largeUnitId' => $product->large_unit_id,
                'conversionRate' => (float) $product->conversion_rate,
                'purchaseUnitId' => $product->purchase_unit_id,
                'location' => $product->location,
                'cost_price' => (float) $product->cost_price,
                'price' => (float) $product->price,
                'member_price' => (float) $product->member_price,
                'wholesale_price' => (float) $product->wholesale_price,
                'is_active' => (bool) $product->is_active,
            ],
            'categories' => Category::where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
            'units' => Unit::all()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
        ]);
    }

    /**
     * 更新儲存體中的指定資源。
     */
    public function update(Request $request, Product $product)
    {
        $validated = $request->validate([
            'code' => 'nullable|unique:products,code,' . $product->id,
            'barcode' => 'nullable|unique:products,barcode,' . $product->id,
            'name' => 'required|string|max:255',
            'category_id' => 'required|exists:categories,id',
            'brand' => 'nullable|string|max:255',
            'specification' => 'nullable|string',
            'base_unit_id' => 'required|exists:units,id',
            'large_unit_id' => 'nullable|exists:units,id',
            'purchase_unit_id' => 'nullable|exists:units,id',
            'conversion_rate' => 'nullable|numeric|min:0',
            'location' => 'nullable|string|max:255',
            'cost_price' => 'nullable|numeric|min:0',
            'price' => 'nullable|numeric|min:0',
            'member_price' => 'nullable|numeric|min:0',
            'wholesale_price' => 'nullable|numeric|min:0',
            'is_active' => 'boolean',
        ]);

        if (empty($validated['code'])) {
            $validated['code'] = $this->generateRandomCode();
        }

        if (empty($validated['barcode'])) {
            $validated['barcode'] = $this->generateRandomBarcode();
        }

        $product->update($validated);

        if ($request->input('from') === 'show') {
            return redirect()->route('products.show', $product->id)->with('success', '商品已更新');
        }

        return redirect()->route('products.index')->with('success', '商品已更新');
    }

    /**
     * 從儲存體中移除指定資源。
     */
    public function destroy(Product $product)
    {
        $product->delete();

        return redirect()->back()->with('success', '商品已刪除');
    }

    /**
     * 下載匯入範本
     */
    public function template()
    {
        return Excel::download(new ProductTemplateExport, 'products_template.xlsx');
    }

    /**
     * 匯入商品
     */
    public function import(Request $request)
    {
        $request->validate([
            'file' => 'required|file|mimes:xlsx,xls',
        ]);

        try {
            Excel::import(new ProductImport, $request->file('file'));
            return redirect()->back()->with('success', '商品匯入成功');
        } catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
            $failures = $e->failures();
            $messages = [];
            foreach ($failures as $failure) {
                $messages[] = '第 ' . $failure->row() . ' 行: ' . implode(', ', $failure->errors());
            }
            return redirect()->back()->withErrors(['file' => implode("\n", $messages)]);
        } catch (\Exception $e) {
            return redirect()->back()->withErrors(['file' => '匯入失敗: ' . $e->getMessage()]);
        }
    }

    /**
     * 生成隨機 8 碼代號 (大寫英文+數字)
     */
    private function generateRandomCode(): string
    {
        $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $code = '';
        
        do {
            $code = '';
            for ($i = 0; $i < 8; $i++) {
                $code .= $characters[rand(0, strlen($characters) - 1)];
            }
        } while (Product::where('code', $code)->exists());

        return $code;
    }

    /**
     * 生成隨機 13 碼條碼 (純數字)
     */
    private function generateRandomBarcode(): string
    {
        $barcode = '';
        
        do {
            $barcode = '';
            for ($i = 0; $i < 13; $i++) {
                $barcode .= rand(0, 9);
            }
        } while (Product::where('barcode', $barcode)->exists());

        return $barcode;
    }
}
