<?php

declare(strict_types=1);

namespace App\Models;

/**
 * Result of PUT /karvipartner/contracts/{contract_id} (Edit contract API).
 */
final class ContractUpdateResult
{
    public function __construct(
        public readonly bool $success,
        public readonly string $message,
        public readonly ?array $contract = null,
        public readonly ?array $apiResponse = null,
        public readonly ?array $refreshedContract = null,
        public readonly int $httpStatus = 0,
        public readonly ?string $errorCode = null,
    ) {
    }

    public static function ok(
        string $message,
        array $apiResponse,
        array $contract,
        ?array $refreshedContract = null,
        int $httpStatus = 200
    ): self {
        return new self(
            success: true,
            message: $message,
            contract: $contract,
            apiResponse: $apiResponse,
            refreshedContract: $refreshedContract,
            httpStatus: $httpStatus,
        );
    }

    /** @param array<string,mixed>|null $apiResponse */
    public static function fail(
        string $message,
        ?array $apiResponse = null,
        int $httpStatus = 400,
        ?string $errorCode = null
    ): self {
        return new self(
            success: false,
            message: $message,
            apiResponse: $apiResponse,
            httpStatus: $httpStatus,
            errorCode: $errorCode,
        );
    }

    public function updatedAt(): string
    {
        return trim((string)($this->contract['updated_at'] ?? ''));
    }
}
