<?php

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

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\ESign\ContractStoreRequest;
use App\Http\Requests\ESign\FieldConfigureRequest;
use App\Services\ESign\EsignContractService;
use App\Services\ESign\EsignPdfService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class EsignContractController extends Controller
{
    public function __construct(
        private EsignContractService $service,
        private EsignPdfService $pdfService,
    ) {}

    public function index(Request $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->list($request->all());
        }, __('message.fetched'));
    }

    public function show(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->show($id);
        }, __('message.fetched'));
    }

    public function store(ContractStoreRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->create($request->validated() + ['file' => $request->file('file')]);
        }, __('message.created'));
    }

    public function cancel(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->cancel($id);
        }, __('message.esign.cancelled'));
    }

    public function configureFields(int $id, FieldConfigureRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->configureFields($id, $request->validated()['fields']);
        }, __('message.esign.fields_configured'));
    }

    public function send(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->send($id);
        }, __('message.esign.sent'));
    }

    public function remind(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->remind($id);
        }, __('message.esign.reminded'));
    }

    public function stats(): JsonResponse
    {
        return ApiResponse::handle(function () {
            return $this->service->stats();
        }, __('message.fetched'));
    }

    public function download(int $id): \Symfony\Component\HttpFoundation\StreamedResponse|JsonResponse
    {
        try {
            $contract = $this->service->show($id);
            $filePath = $contract->signed_file_path ?? $contract->original_file_path;

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

            $fileName = $contract->original_file_name ?? 'contract.pdf';

            return Storage::disk('local')->download($filePath, $fileName);
        } catch (\Throwable $e) {
            return ApiResponse::error($e->getMessage(), 500);
        }
    }

    public function verify(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            $contract = $this->service->show($id);

            if (! $contract->original_file_path || ! $contract->original_file_hash) {
                return ['verified' => false, 'message' => '파일 정보가 없습니다.'];
            }

            $isValid = $this->pdfService->verifyIntegrity(
                $contract->original_file_path,
                $contract->original_file_hash
            );

            return [
                'verified' => $isValid,
                'original_hash' => $contract->original_file_hash,
            ];
        }, __('message.esign.verified'));
    }
}
