<?php

namespace PortalPeople\virtualstock\models;

/**
 * Represents a VirtualStock retailer as returned by the REST API.
 * This is a plain value object — not an ActiveRecord or DynamicModel.
 */
class Retailer
{
    /** Retailer ID as assigned by VirtualStock. */
    public string $id = '';

    /** Human-readable retailer name. */
    public string $name = '';

    /** Retailer code / short identifier. */
    public string $code = '';

    /** Primary contact email address. */
    public ?string $email = null;

    /** Primary contact telephone number. */
    public ?string $phone = null;

    /**
     * Whether the retailer account is currently active on the platform.
     * Inactive retailers should not receive new orders.
     */
    public bool $active = true;

    /**
     * Retailer-specific order reference format/prefix.
     * Some retailers (e.g. Screwfix) use long numeric order references.
     */
    public ?string $orderReferenceFormat = null;

    /** ISO 8601 creation timestamp. */
    public ?string $createdAt = null;

    /** ISO 8601 last-updated timestamp. */
    public ?string $updatedAt = null;

    /** Raw API payload for any fields not explicitly mapped. */
    public array $raw = [];

    // -------------------------------------------------------------------------

    public static function fromArray(array $data): self
    {
        $retailer = new self();
        $retailer->id                  = (string) ($data['id']                     ?? $data['retailer_id'] ?? '');
        $retailer->name                = (string) ($data['name']                   ?? $data['retailer_name'] ?? '');
        $retailer->code                = (string) ($data['code']                   ?? $data['retailer_code'] ?? '');
        $retailer->email               = isset($data['email'])                     ? (string) $data['email'] : null;
        $retailer->phone               = isset($data['phone'])                     ? (string) $data['phone'] : null;
        $retailer->active              = (bool)   ($data['active']                 ?? $data['is_active'] ?? true);
        $retailer->orderReferenceFormat = isset($data['order_reference_format'])
            ? (string) $data['order_reference_format']
            : (isset($data['orderReferenceFormat']) ? (string) $data['orderReferenceFormat'] : null);
        $retailer->createdAt           = $data['created_at'] ?? $data['createdAt'] ?? null;
        $retailer->updatedAt           = $data['updated_at'] ?? $data['updatedAt'] ?? null;
        $retailer->raw                 = $data;

        return $retailer;
    }

    public function toArray(): array
    {
        return [
            'id'                     => $this->id,
            'name'                   => $this->name,
            'code'                   => $this->code,
            'email'                  => $this->email,
            'phone'                  => $this->phone,
            'active'                 => $this->active,
            'order_reference_format' => $this->orderReferenceFormat,
            'created_at'             => $this->createdAt,
            'updated_at'             => $this->updatedAt,
        ];
    }

    /**
     * Returns true if the retailer has enough data to be usable in order operations.
     */
    public function isIdentifiable(): bool
    {
        return $this->id !== '' && $this->name !== '';
    }

    /**
     * Some retailers (notably Screwfix) use long purely-numeric order references
     * that exceed standard integer range. This returns true when the retailer
     * name suggests that pattern, or when an explicit format hint is set.
     */
    public function usesLongOrderReferences(): bool
    {
        if ($this->orderReferenceFormat !== null) {
            return str_contains(strtolower($this->orderReferenceFormat), 'long')
                || str_contains($this->orderReferenceFormat, '%d');
        }

        return stripos($this->name, 'screwfix') !== false;
    }
}
