<?php

namespace App\Http\Controllers;

use App\Models\Investment;
use App\Models\Investor;
use App\Models\Car;

use Illuminate\Http\Request;

class InvestmentController extends Controller
{
    // Show all investments
    public function index(Request $request)
    {
        $query = Investment::with('investor');
    
        if ($request->filled('investor_id')) {
            $query->where('investor_id', $request->investor_id);
        }
    
        if ($request->filled('type')) {
            $query->where('type', $request->type);
        }
    
        if ($request->filled('date')) {
            $query->whereDate('date', $request->date);
        }
    
        // $investments = $query->latest()->paginate(10);
        $investments = $query->latest()->paginate(999999999);

    
        // get investors for dropdown
        $investors = Investor::all();
    
        return view('investments.index', compact('investments', 'investors'));
    }

    // Show create form
    public function create()
    {
        $investors = Investor::all();
        return view('investments.create', compact('investors'));
    }

    // Store new investment
    public function store(Request $request)
    {
        $request->validate([
            'investor_id' => 'required|exists:investors,id',
            'type' => 'required|string|max:255',
            'amount' => 'required|numeric|min:0',
            'date' => 'required|date',
            'remarks' => 'nullable|string',
        ]);

        Investment::create($request->all());

        return redirect()->route('investments.index')
            ->with('success', 'Investment added successfully.');
    }

    // Show edit form
    public function edit(Investment $investment)
    {
        $investment->load('investor'); // no need to load investor.cars now
        $investors = Investor::with('investments')->get();
    
        // Fetch all active cars
        $cars = Car::where('status', 1)->get();
    
        return view('investments.edit', compact('investment', 'investors', 'cars'));
    }
    
    

    // Update investment
    public function update(Request $request, Investment $investment)
    {
        $request->validate([
            'investor_id' => 'required|exists:investors,id',
            'type' => 'required|string|max:255',
            'amount' => 'required|numeric|min:0',
            'date' => 'required|date',
            'remarks' => 'nullable|string',
            'car_id' => 'nullable|exists:cars,id', // validate car_id if provided
        ]);
    
        // Prepare data for update
        $data = $request->only(['investor_id', 'type', 'amount', 'date', 'remarks']);
    
        // If type = car, include car_id; otherwise set it null
        if ($request->type === 'car') {
            $data['car_id'] = $request->car_id;
        } else {
            $data['car_id'] = null;
        }
    
        // Update record
        $investment->update($data);
    
        return redirect()->route('investments.index')
            ->with('success', 'Investment updated successfully.');
    }
    

    // Delete investment
    public function destroy(Investment $investment)
    {
        $investment->delete();

        return redirect()->route('investments.index')
            ->with('success', 'Investment deleted successfully.');
    }
}
