<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\CompanyAsset;
use Illuminate\Http\Request;

class CompanyAssetController extends Controller
{
    public function index(Request $request)
    {
        $user = $request->user();
        // Allow Super Admin (no company_id) to manage assets for the system holding company
        $companyId = $user->company_id;
        if (!$companyId) {
            if ($user->isSuperAdmin()) {
                $companyId = \App\Models\Company::systemHolding()->id;
            } else {
                return response()->json(['message' => 'No company'], 403);
            }
        }

        $query = CompanyAsset::where('company_id', $companyId);
        if ($request->filled('status')) $query->where('status', $request->status);
        if ($request->filled('category')) $query->where('category', $request->category);

        return response()->json(['data' => $query->latest()->get()]);
    }

    public function store(Request $request)
    {
        $user = $request->user();
        // Allow Super Admin to create assets under the system holding company
        $companyId = $user->company_id;
        if (!$companyId) {
            if ($user->isSuperAdmin()) {
                $companyId = \App\Models\Company::systemHolding()->id;
            } else {
                return response()->json(['message' => 'No company'], 403);
            }
        }

        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'category' => 'required|in:real_estate,equipment,vehicle,furniture,technology,inventory,investment,intangible,other',
            'purchase_value' => 'required|numeric|min:0',
            'current_value' => 'nullable|numeric|min:0',
            'purchase_date' => 'nullable|date',
            'vendor_name' => 'nullable|string|max:255',
            'location' => 'nullable|string|max:255',
            'notes' => 'nullable|string',
            'receipt' => 'nullable|file|mimes:pdf,jpg,jpeg,png|max:2048',
        ]);

        $validated['company_id'] = $companyId;
        $validated['created_by'] = $user->id;
        $validated['asset_number'] = 'AST-' . strtoupper(uniqid());

        if ($request->hasFile('receipt')) {
            $path = $request->file('receipt')->store('asset_receipts', 'public');
            $validated['receipt_path'] = $path;
        }

        $asset = CompanyAsset::create($validated);
        return response()->json(['data' => $asset, 'message' => 'Asset added'], 201);
    }

    public function update(Request $request, $id)
    {
        $user = $request->user();
        $companyId = $user->company_id ?: ($user->isSuperAdmin() ? \App\Models\Company::systemHolding()->id : null);
        if (!$companyId) return response()->json(['message' => 'No company'], 403);
        $asset = CompanyAsset::where('company_id', $companyId)->findOrFail($id);

        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'category' => 'required|in:real_estate,equipment,vehicle,furniture,technology,inventory,investment,intangible,other',
            'purchase_value' => 'required|numeric|min:0',
            'current_value' => 'nullable|numeric|min:0',
            'purchase_date' => 'nullable|date',
            'vendor_name' => 'nullable|string|max:255',
            'location' => 'nullable|string|max:255',
            'notes' => 'nullable|string',
            'status' => 'required|in:active,sold,disposed,maintenance,retired',
            'receipt' => 'nullable|file|mimes:pdf,jpg,jpeg,png|max:2048',
        ]);

        if ($request->hasFile('receipt')) {
            $path = $request->file('receipt')->store('asset_receipts', 'public');
            $validated['receipt_path'] = $path;
        }

        $asset->update($validated);
        return response()->json(['data' => $asset, 'message' => 'Asset updated']);
    }

    public function destroy(Request $request, $id)
    {
        $user = $request->user();
        $companyId = $user->company_id ?: ($user->isSuperAdmin() ? \App\Models\Company::systemHolding()->id : null);
        if (!$companyId) return response()->json(['message' => 'No company'], 403);
        $asset = CompanyAsset::where('company_id', $companyId)->findOrFail($id);
        $asset->delete();
        return response()->json(['message' => 'Asset deleted']);
    }
}
