<?php

namespace Modules\Accounts\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\Accounts\Http\Requests\Voucher\PostVoucherRequest;
use Modules\Accounts\Http\Requests\Voucher\ReverseVoucherRequest;
use Modules\Accounts\Http\Requests\Voucher\StoreVoucherRequest;
use Modules\Accounts\Http\Requests\Voucher\UpdateVoucherRequest;
use Modules\Accounts\Models\AccountVoucher;
use Modules\Accounts\Models\ChartOfAccount;
use Modules\Accounts\Services\TransactionService;
use Modules\Accounts\Services\VoucherService;
use Modules\Accounts\Transformers\TransactionResource;
use Modules\Accounts\Transformers\VoucherResource;
use Modules\Core\Helpers\ApiResponse;

class VoucherController extends Controller
{
    public function __construct(
        protected VoucherService $vouchers,
        protected TransactionService $transactions
    ) {
    }

    public function index(Request $request)
    {
        return VoucherResource::collection($this->vouchers->list($request->all()))->additional([
            'status' => true,
            'message' => 'Vouchers retrieved successfully.',
        ]);
    }

    public function store(StoreVoucherRequest $request)
    {
        return (new VoucherResource($this->vouchers->store($request->validated(), auth()->id())))->additional([
            'status' => true,
            'message' => 'Voucher created successfully.',
        ]);
    }

    public function show(AccountVoucher $voucher)
    {
        return (new VoucherResource($this->vouchers->show($voucher->id)))->additional([
            'status' => true,
            'message' => 'Voucher retrieved successfully.',
        ]);
    }

    public function update(UpdateVoucherRequest $request, AccountVoucher $voucher)
    {
        return (new VoucherResource($this->vouchers->update($voucher, $request->validated())))->additional([
            'status' => true,
            'message' => 'Voucher updated successfully.',
        ]);
    }

    public function destroy(AccountVoucher $voucher)
    {
        $this->vouchers->delete($voucher);

        return ApiResponse::successResponse('Voucher deleted successfully.');
    }

    public function postVoucher(PostVoucherRequest $request, AccountVoucher $voucher)
    {
        return (new VoucherResource($this->vouchers->post($voucher, auth()->id())))->additional([
            'status' => true,
            'message' => 'Voucher posted successfully.',
        ]);
    }

    public function reverseVoucher(ReverseVoucherRequest $request, AccountVoucher $voucher)
    {
        return (new VoucherResource($this->vouchers->reverse($voucher, $request->validated(), auth()->id())))->additional([
            'status' => true,
            'message' => 'Voucher reversed successfully.',
        ]);
    }

    public function trialBalance(Request $request)
    {
        return ApiResponse::successResponse('Trial balance retrieved successfully.', 200, $this->transactions->trialBalance(
            $request->input('from_date'),
            $request->input('to_date')
        ));
    }

    public function generalLedger(Request $request)
    {
        return TransactionResource::collection($this->transactions->generalLedger($request->all()))->additional([
            'status' => true,
            'message' => 'General ledger retrieved successfully.',
        ]);
    }

    public function accountStatement(Request $request, ChartOfAccount $account)
    {
        return TransactionResource::collection($this->transactions->accountStatement(
            $account->id,
            $request->input('from_date'),
            $request->input('to_date')
        ))->additional([
            'status' => true,
            'message' => 'Account statement retrieved successfully.',
        ]);
    }
}
