<?php

namespace App\Http\Controllers;

use App\Models\Shareholder;
use App\Models\ShareholderTransaction;
use Illuminate\Http\Request;

class ShareholderController extends Controller
{
    public function index()
    {
        $shareholders = Shareholder::orderBy('name')->get();
        return view('shareholders.index', compact('shareholders'));
    }

    public function create()
    {
        return view('shareholders.create');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name'    => 'required|string|max:191',
            'phone'   => 'nullable|string|max:50',
            'email'   => 'nullable|email|max:191',
            'address' => 'nullable|string|max:500',
        ]);

        Shareholder::create($data);

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

    public function show(Shareholder $shareholder)
    {
        $transactions = $shareholder->transactions()->paginate(20);
        return view('shareholders.show', compact('shareholder', 'transactions'));
    }

    public function edit(Shareholder $shareholder)
    {
        return view('shareholders.edit', compact('shareholder'));
    }

    public function update(Request $request, Shareholder $shareholder)
    {
        $data = $request->validate([
            'name'      => 'required|string|max:191',
            'phone'     => 'nullable|string|max:50',
            'email'     => 'nullable|email|max:191',
            'address'   => 'nullable|string|max:500',
            'is_active' => 'boolean',
        ]);

        $shareholder->update($data + ['is_active' => $request->boolean('is_active')]);

        return redirect()->route('shareholders.index')->with('success', 'Shareholder updated.');
    }

    public function destroy(Shareholder $shareholder)
    {
        $shareholder->delete();
        return back()->with('success', 'Shareholder deleted.');
    }

    // ─── Transactions ─────────────────────────────────────────────────────────

    public function transactionStore(Request $request, Shareholder $shareholder)
    {
        $data = $request->validate([
            'transaction_date' => 'required|date',
            'type'             => 'required|in:investment,withdrawal,profit,profit_withdrawal',
            'amount'           => 'required|numeric|min:0.01',
            'notes'            => 'nullable|string|max:500',
        ]);

        $shareholder->transactions()->create($data);

        // Update running totals
        match($data['type']) {
            'investment'        => $shareholder->increment('total_investment',        $data['amount']),
            'withdrawal'        => $shareholder->increment('total_withdrawal',        $data['amount']),
            'profit'            => $shareholder->increment('total_profit',            $data['amount']),
            'profit_withdrawal' => $shareholder->increment('total_profit_withdrawal', $data['amount']),
        };

        return back()->with('success', 'Transaction recorded.');
    }

    public function transactionDestroy(Shareholder $shareholder, ShareholderTransaction $transaction)
    {
        // Reverse the running total
        match($transaction->type) {
            'investment'        => $shareholder->decrement('total_investment',        $transaction->amount),
            'withdrawal'        => $shareholder->decrement('total_withdrawal',        $transaction->amount),
            'profit'            => $shareholder->decrement('total_profit',            $transaction->amount),
            'profit_withdrawal' => $shareholder->decrement('total_profit_withdrawal', $transaction->amount),
        };

        $transaction->delete();

        return back()->with('success', 'Transaction deleted.');
    }
}
