<?php

namespace App\Exceptions;

use Exception;

/**
 * 품목 코드 중복 예외
 *
 * 중복된 품목 ID를 함께 반환하기 위한 커스텀 예외
 */
class DuplicateCodeException extends Exception
{
    protected int $duplicateId;

    protected string $duplicateCode;

    public function __construct(string $code, int $duplicateId, ?string $message = null)
    {
        $this->duplicateCode = $code;
        $this->duplicateId = $duplicateId;

        parent::__construct($message ?? __('error.item.duplicate_code'));
    }

    public function getDuplicateId(): int
    {
        return $this->duplicateId;
    }

    public function getDuplicateCode(): string
    {
        return $this->duplicateCode;
    }

    /**
     * HTTP 응답으로 변환
     */
    public function render()
    {
        return response()->json([
            'success' => false,
            'message' => $this->getMessage(),
            'error' => [
                'code' => 400,
            ],
            'duplicate_id' => $this->duplicateId,
            'duplicate_code' => $this->duplicateCode,
        ], 400);
    }
}
