<?php

namespace App\Http\Controllers\Admin;

use App\Core\Repositories\ChartOfAccountRepository;
use App\Core\Services\Accounting\ChartOfAccountService;
use App\Http\Controllers\Controller;
use App\Models\ChartOfAccount;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ChartOfAccountController extends Controller
{
    public function __construct(
        private ChartOfAccountService $service,
        private ChartOfAccountRepository $repository
    ) {}

    /**
     * Display a listing of accounts
     */
    public function index(Request $request): View
    {
        $filters = [
            'type' => $request->get('type'),
            'category' => $request->get('category'),
            'is_active' => $request->get('is_active'),
            'search' => $request->get('search'),
        ];

        $accounts = $this->repository->getPaginated(15, $filters);

        return view('admin.accounting.chart-of-accounts.index', [
            'accounts' => $accounts,
            'filters' => $filters,
        ]);
    }

    /**
     * Show the form for creating a new account
     */
    public function create(): View
    {
        $parentAccounts = $this->repository->getAll();

        return view('admin.accounting.chart-of-accounts.create', [
            'parentAccounts' => $parentAccounts,
        ]);
    }

    /**
     * Store a newly created account
     */
    public function store(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'code' => 'required|string|max:50',
            'name' => 'required|string|max:255',
            'type' => 'required|in:asset,liability,equity,revenue,expense',
            'category' => 'nullable|string',
            'description' => 'nullable|string',
            'opening_balance' => 'nullable|numeric',
            'parent_id' => 'nullable|exists:chart_of_accounts,id',
            'sort_order' => 'nullable|integer',
        ]);

        $this->service->create($validated);

        return redirect()
            ->route('admin.chart-of-accounts.index')
            ->with('success', 'Account created successfully.');
    }

    /**
     * Show the form for editing the account
     */
    public function edit(ChartOfAccount $chartOfAccount): View
    {
        $parentAccounts = $this->repository->getAll();

        return view('admin.accounting.chart-of-accounts.edit', [
            'account' => $chartOfAccount,
            'parentAccounts' => $parentAccounts,
        ]);
    }

    /**
     * Update the account
     */
    public function update(Request $request, ChartOfAccount $chartOfAccount): RedirectResponse
    {
        $validated = $request->validate([
            'code' => 'required|string|max:50',
            'name' => 'required|string|max:255',
            'type' => 'required|in:asset,liability,equity,revenue,expense',
            'category' => 'nullable|string',
            'description' => 'nullable|string',
            'is_active' => 'nullable|boolean',
            'parent_id' => 'nullable|exists:chart_of_accounts,id',
            'sort_order' => 'nullable|integer',
        ]);

        $this->service->update($chartOfAccount, $validated);

        return redirect()
            ->route('admin.chart-of-accounts.index')
            ->with('success', 'Account updated successfully.');
    }

    /**
     * Remove the account
     */
    public function destroy(ChartOfAccount $chartOfAccount): RedirectResponse
    {
        $this->service->delete($chartOfAccount);

        return redirect()
            ->route('admin.chart-of-accounts.index')
            ->with('success', 'Account deleted successfully.');
    }
}
