<?php

namespace App\Http\Controllers\Api\Discounts;

use App\Http\Controllers\Api\BaseApiController;
use App\Http\Requests\Discounts\ApplyDiscountVoucherRequest;
use App\Http\Requests\Discounts\StoreDiscountVoucherRequest;
use App\Http\Requests\Discounts\UpdateDiscountVoucherRequest;
use App\Http\Resources\Discounts\DiscountParentResource;
use App\Http\Resources\Discounts\DiscountVoucherResource;
use App\Services\Discounts\DiscountApplyService;
use App\Services\Discounts\DiscountContextService;
use App\Services\Discounts\DiscountParentService;
use App\Services\Discounts\DiscountVoucherService;
use Illuminate\Http\JsonResponse;

class DiscountController extends BaseApiController
{
    private DiscountContextService $context;
    private DiscountVoucherService $voucherService;
    private DiscountParentService $parentService;
    private DiscountApplyService $applyService;

    public function __construct(
        DiscountContextService $context,
        DiscountVoucherService $voucherService,
        DiscountParentService $parentService,
        DiscountApplyService $applyService
    ) {
        parent::__construct();
        $this->context = $context;
        $this->voucherService = $voucherService;
        $this->parentService = $parentService;
        $this->applyService = $applyService;
    }

    public function options(): JsonResponse
    {
        $schoolYear = $this->context->getSchoolYear();
        $vouchers = $this->voucherService->listActive();
        $parents = $this->parentService->listParentsWithDiscounts($schoolYear);

        return response()->json([
            'ok' => true,
            'vouchers' => DiscountVoucherResource::collection($vouchers),
            'parents' => DiscountParentResource::collection($parents),
            'schoolYear' => $schoolYear,
        ]);
    }

    public function apply(ApplyDiscountVoucherRequest $request): JsonResponse
    {
        $payload = $request->validated();
        $result = $this->applyService->applyVoucher(
            (int) $payload['voucher_id'],
            $payload['parent_ids'],
            (bool) ($payload['allow_additional'] ?? false),
            (int) (auth()->id() ?? 0)
        );

        return response()->json($result, $result['ok'] ? 200 : 422);
    }

    public function listVouchers(): JsonResponse
    {
        $vouchers = $this->voucherService->listAll();
        return response()->json([
            'ok' => true,
            'vouchers' => DiscountVoucherResource::collection($vouchers),
        ]);
    }

    public function storeVoucher(StoreDiscountVoucherRequest $request): JsonResponse
    {
        $voucher = $this->voucherService->create($request->validated());
        return response()->json([
            'ok' => true,
            'voucher' => new DiscountVoucherResource($voucher),
        ], 201);
    }

    public function updateVoucher(UpdateDiscountVoucherRequest $request, int $id): JsonResponse
    {
        $voucher = $this->voucherService->update($id, $request->validated());
        return response()->json([
            'ok' => true,
            'voucher' => new DiscountVoucherResource($voucher),
        ]);
    }

    public function showVoucher(int $id): JsonResponse
    {
        $voucher = $this->voucherService->find($id);
        return response()->json([
            'ok' => true,
            'voucher' => new DiscountVoucherResource($voucher),
        ]);
    }

    public function deleteVoucher(int $id): JsonResponse
    {
        $this->voucherService->delete($id);
        return response()->json([
            'ok' => true,
        ]);
    }
}
