<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\Subscription\ExportStoreRequest;
use App\Http\Requests\V1\Subscription\SubscriptionCancelRequest;
use App\Http\Requests\V1\Subscription\SubscriptionIndexRequest;
use App\Http\Requests\V1\Subscription\SubscriptionStoreRequest;
use App\Services\SubscriptionService;
use Illuminate\Http\JsonResponse;

class SubscriptionController extends Controller
{
    public function __construct(
        private readonly SubscriptionService $subscriptionService
    ) {}

    /**
     * 구독 목록
     */
    public function index(SubscriptionIndexRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->subscriptionService->index($request->validated()),
            __('message.fetched')
        );
    }

    /**
     * 현재 활성 구독
     */
    public function current(): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->subscriptionService->current(),
            __('message.fetched')
        );
    }

    /**
     * 구독 등록
     */
    public function store(SubscriptionStoreRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->subscriptionService->store($request->validated()),
            __('message.created')
        );
    }

    /**
     * 구독 상세
     */
    public function show(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->subscriptionService->show($id),
            __('message.fetched')
        );
    }

    /**
     * 구독 취소
     */
    public function cancel(SubscriptionCancelRequest $request, int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->subscriptionService->cancel($id, $request->validated()['reason'] ?? null),
            __('message.subscription.cancelled')
        );
    }

    /**
     * 구독 갱신
     */
    public function renew(SubscriptionStoreRequest $request, int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->subscriptionService->renew($id, $request->validated()),
            __('message.subscription.renewed')
        );
    }

    /**
     * 구독 일시정지
     */
    public function suspend(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->subscriptionService->suspend($id),
            __('message.subscription.suspended')
        );
    }

    /**
     * 구독 재개
     */
    public function resume(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->subscriptionService->resume($id),
            __('message.subscription.resumed')
        );
    }

    /**
     * 사용량 조회
     */
    public function usage(): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->subscriptionService->usage(),
            __('message.fetched')
        );
    }

    /**
     * 내보내기 요청
     */
    public function export(ExportStoreRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->subscriptionService->createExport($request->validated()),
            __('message.export.requested')
        );
    }

    /**
     * 내보내기 상태 조회
     */
    public function exportStatus(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->subscriptionService->getExport($id),
            __('message.fetched')
        );
    }
}
