<?php

declare(strict_types=1);

namespace App\Services\Identity;

use Illuminate\Support\Str;

class Siren24Provider implements IdentityProviderInterface
{
    public function __construct(private readonly array $config = [])
    {
    }

    public function provider(): string
    {
        return 'siren24';
    }

    public function isMock(): bool
    {
        return (bool) ($this->config['mock'] ?? false);
    }

    public function buildRequest(array $context = []): array
    {
        $requestId = 'S24-'.Str::ulid()->toString();
        if ($this->isMock()) {
            return [
                'request_id' => $requestId,
                'standard_url' => url('/api/v1/identity/mock-window?request_id='.$requestId.'&provider=siren24'),
                'mock' => true,
            ];
        }
        return [
            'request_id' => $requestId,
            'standard_url' => 'https://www.siren24.com/...',
        ];
    }

    public function parseCallback(array $payload): array
    {
        if ($this->isMock()) {
            return [
                'status' => 'verified',
                'ci' => 'MOCK-S24-CI-'.hash('sha256', json_encode($payload)),
                'di' => 'MOCK-S24-DI-'.hash('sha256', $payload['request_id'] ?? Str::random(8)),
                'name' => $payload['name'] ?? '홍길동',
                'birthdate' => '1990-01-01',
                'gender' => 'male',
                'mobile' => '01098765432',
                'carrier' => 'kt',
                'raw' => array_merge(['mock' => true], $payload),
            ];
        }
        return [
            'status' => 'failed',
            'ci' => null,
            'di' => null,
            'raw' => $payload,
        ];
    }
}
