<?php

namespace App\Services\Fcm;

class FcmResponse
{
    public function __construct(
        public readonly bool $success,
        public readonly ?string $messageId,
        public readonly ?string $error,
        public readonly int $statusCode,
        public readonly array $rawResponse
    ) {}

    /**
     * 성공 응답 생성
     */
    public static function success(?string $messageId, int $statusCode, array $rawResponse): self
    {
        return new self(
            success: true,
            messageId: $messageId,
            error: null,
            statusCode: $statusCode,
            rawResponse: $rawResponse
        );
    }

    /**
     * 실패 응답 생성
     */
    public static function failure(string $error, int $statusCode, array $rawResponse): self
    {
        return new self(
            success: false,
            messageId: null,
            error: $error,
            statusCode: $statusCode,
            rawResponse: $rawResponse
        );
    }

    /**
     * FCM 에러 코드 추출
     */
    public function getErrorCode(): ?string
    {
        if ($this->success) {
            return null;
        }

        return $this->rawResponse['error']['details'][0]['errorCode'] ?? null;
    }

    /**
     * 토큰이 유효하지 않은지 확인 (삭제 필요)
     */
    public function isInvalidToken(): bool
    {
        if ($this->success) {
            return false;
        }

        // FCM에서 반환하는 무효 토큰 에러 코드들
        $invalidTokenErrors = [
            'UNREGISTERED',
            'INVALID_ARGUMENT',
            'NOT_FOUND',
        ];

        return in_array($this->getErrorCode(), $invalidTokenErrors, true);
    }

    /**
     * 배열로 변환
     */
    public function toArray(): array
    {
        return [
            'success' => $this->success,
            'message_id' => $this->messageId,
            'error' => $this->error,
            'status_code' => $this->statusCode,
        ];
    }
}
