<?php

namespace App\Http\Controllers\Portal;

use App\Http\Controllers\Controller;
use App\Models\Account;
use App\Models\Customer;
use Illuminate\View\View;

class AccountController extends Controller
{
    public function index(): View
    {
        $customer = Customer::query()->findOrFail(session('portal_customer_id'));

        $accounts = Account::query()
            ->where('customer_id', $customer->id)
            ->latest()
            ->paginate(15);

        return view('portal.accounts.index', compact('customer', 'accounts'));
    }

    public function show(Account $account): View
    {
        $customer = Customer::query()->findOrFail(session('portal_customer_id'));

        abort_if($account->customer_id !== $customer->id, 403);

        $transactions = $account->transactions()->latest()->paginate(25);

        return view('portal.accounts.show', compact('customer', 'account', 'transactions'));
    }
}
