<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\LedgerEntryResource;
use App\Models\LedgerEntry;
use App\Services\LedgerService;
use Illuminate\Http\Request;

class LedgerController extends Controller
{
    protected $ledgerService;

    public function __construct(LedgerService $ledgerService)
    {
        $this->ledgerService = $ledgerService;
    }

    public function index(Request $request)
    {
        $query = LedgerEntry::with(['chartOfAccount', 'currency', 'creator']);

        if ($request->has('account_id')) {
            $query->where('chart_of_account_id', $request->account_id);
        }

        if ($request->has('type')) {
            $query->whereHas('chartOfAccount', function ($q) use ($request) {
                $q->where('type', $request->type);
            });
        }

        if ($request->has('date_from')) {
            $query->whereDate('entry_date', '>=', $request->date_from);
        }

        if ($request->has('date_to')) {
            $query->whereDate('entry_date', '<=', $request->date_to);
        }

        $entries = $query->orderByDesc('entry_date')->paginate($request->per_page ?? 20);

        return LedgerEntryResource::collection($entries);
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'entry_date' => 'required|date',
            'chart_of_account_id' => 'required|exists:chart_of_accounts,id',
            'currency_id' => 'required|exists:currencies,id',
            'amount' => 'required|numeric',
            'description' => 'nullable|string',
        ]);

        $data['created_by'] = auth()->id();
        $data['source_type'] = 'manual';

        $entry = LedgerEntry::create($data);

        return new LedgerEntryResource($entry);
    }

    public function summary(Request $request)
    {
        $request->validate([
            'date_from' => 'required|date',
            'date_to' => 'required|date|after_or_equal:date_from',
        ]);

        $report = $this->ledgerService->getProfitLossReport(
            $request->date_from,
            $request->date_to
        );

        return response()->json($report);
    }

    public function incomeSummary(Request $request)
    {
        $request->validate([
            'date_from' => 'required|date',
            'date_to' => 'required|date|after_or_equal:date_from',
        ]);

        $summary = $this->ledgerService->getIncomeSummary(
            $request->date_from,
            $request->date_to
        );

        return response()->json($summary);
    }

    public function expenseSummary(Request $request)
    {
        $request->validate([
            'date_from' => 'required|date',
            'date_to' => 'required|date|after_or_equal:date_from',
        ]);

        $summary = $this->ledgerService->getExpenseSummary(
            $request->date_from,
            $request->date_to
        );

        return response()->json($summary);
    }
}
