<?php

namespace AC_SYNC\Includes\Classes\Differential\Entities;

if (!defined('ABSPATH'))
    exit; // Exit if accessed directly
use AC_SYNC\Includes\Classes\Differential\Schema\Action_Wear_Schema as Schema;
use AC_SYNC\Includes\Classes\Differential\Schema\Action_Wear_Schema_Interface as SchemaInterface;

abstract class Action_Wear_Entities
{

    private $apiSchema;

    public function setApiSchema($schema): void
    {
        $this->apiSchema = $schema;
    }

    /**
     * Normalize API response to a flat array of items.
     * Handles multiple API response formats:
     * - flat: [{sku:..}, {sku:..}]
     * - wrapped: [[{sku:..}, {sku:..}]]
     * - keyed by sku: {sku1: {..}, sku2: {..}}
     *
     * @param array $decoded Decoded JSON response
     * @return array|null Normalized items array, or null if empty
     */
    protected static function normalizeApiResponse($decoded)
    {
        if (!is_array($decoded) || count($decoded) === 0) {
            return null;
        }

        if (isset($decoded[0]) && is_array($decoded[0]) && isset($decoded[0]['sku'])) {
            return $decoded; // flat: [{sku:..}, {sku:..}]
        } elseif (isset($decoded[0]) && is_array($decoded[0])) {
            return $decoded[0]; // wrapped: [[{sku:..}, {sku:..}]]
        }

        return $decoded; // keyed by sku: {sku1: {..}, sku2: {..}}
    }

    public function getUpdateTiming()
    {
        return 0;
    }

    public function execute()
    {
        return null;
    }

    private function getClassName()
    {
        $class = get_class($this);
        $class = str_replace("Action_Wear_", "", $class);
        $class = explode("\\", $class);
        $class = end($class);
        $class = strtoupper($class);
        return $class;
    }

    public function getExecutionTime(): int
    {
        return (int) get_option("_ACTIONWEAR_LAST_" . $this->getClassName() . "_UPDATE", time() - (3600 * 48));
    }

    public function updateExecutionTime(): bool
    {
        return update_option("_ACTIONWEAR_LAST_" . $this->getClassName() . "_UPDATE", time());
    }

    public function conformToSchema(SchemaInterface $schema): bool
    {
        if (!$this->apiSchema)
            throw new \Exception("No schema defined");
        $schema = new Schema($schema);
        return $schema->isValidSchema($this->apiSchema);
    }
}
