<?php

namespace App\Http\Controllers\Api\V1\ESign;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\ESign\SignRejectRequest;
use App\Http\Requests\ESign\SignSubmitRequest;
use App\Services\ESign\EsignSignService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class EsignSignController extends Controller
{
    public function __construct(
        private EsignSignService $service,
    ) {}

    public function getContract(string $token): JsonResponse
    {
        return ApiResponse::handle(function () use ($token) {
            return $this->service->getByToken($token);
        }, __('message.fetched'));
    }

    public function sendOtp(string $token): JsonResponse
    {
        return ApiResponse::handle(function () use ($token) {
            return $this->service->sendOtp($token);
        }, __('message.esign.otp_sent'));
    }

    public function verifyOtp(string $token, Request $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($token, $request) {
            $request->validate(['otp_code' => 'required|string|size:6']);

            return $this->service->verifyOtp($token, $request->input('otp_code'));
        }, __('message.esign.otp_verified'));
    }

    public function getDocument(string $token): \Symfony\Component\HttpFoundation\StreamedResponse|JsonResponse
    {
        try {
            $data = $this->service->getByToken($token);
            $contract = $data['contract'];
            $filePath = $contract->original_file_path;

            if (! $filePath || ! Storage::disk('local')->exists($filePath)) {
                return ApiResponse::error(__('error.esign.file_not_found'), 404);
            }

            return Storage::disk('local')->response($filePath, null, [
                'Content-Type' => 'application/pdf',
            ]);
        } catch (\Throwable $e) {
            return ApiResponse::error($e->getMessage(), $e->getCode() ?: 500);
        }
    }

    public function submit(string $token, SignSubmitRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($token, $request) {
            return $this->service->submitSignature($token, $request->validated());
        }, __('message.esign.signed'));
    }

    public function reject(string $token, SignRejectRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($token, $request) {
            return $this->service->reject($token, $request->validated()['reason']);
        }, __('message.esign.rejected'));
    }
}
