<?php

namespace modules\selfserve\classes;

class selfserve_lane_command_arguments
{
    public ?string $license_plate = null;
    public ?int $customer_number = null;
    public ?int $subuser_id = null;
    public ?string $wash_mode = null;
    public bool $defer_relay_side_effects = false;

    /**
     * Set the license plate of the vehicle currently in the lane
     * @param string|null $license_plate
     * @return $this
     */
    public function setLicensePlate(?string $license_plate): self
    {
        $this->license_plate = $license_plate ? strtoupper(trim($license_plate)) : null;
        return $this;
    }

    public function setCustomerNumber(?int $customer_number): self
    {
        $this->customer_number = $customer_number;
        return $this;
    }

    public function setSubuserId(?int $subuser_id): self
    {
        $this->subuser_id = $subuser_id !== null && $subuser_id > 0 ? $subuser_id : null;
        return $this;
    }

    public function setWashMode(?string $wash_mode): self
    {
        $normalized = strtolower(trim((string)$wash_mode));
        if ($wash_mode === null || $normalized === '') {
            $this->wash_mode = null;
            return $this;
        }

        if (!in_array($normalized, ['manual', 'machine'], true)) {
            throw new \InvalidArgumentException('Invalid wash type: ' . $wash_mode);
        }

        $this->wash_mode = $normalized;
        return $this;
    }

    public function setDeferRelaySideEffects(bool $defer_relay_side_effects): self
    {
        $this->defer_relay_side_effects = $defer_relay_side_effects;
        return $this;
    }

    public function setParameters($params): self
    {
        if (is_array($params)) {
            if (array_key_exists('license_plate', $params)) {
                $this->setLicensePlate($params['license_plate']);
            }
            if (array_key_exists('customer_number', $params)) {
                $this->setCustomerNumber($params['customer_number']);
            }
            if (array_key_exists('subuser_id', $params)) {
                $this->setSubuserId($params['subuser_id'] === null ? null : (int)$params['subuser_id']);
            }
            if (array_key_exists('wash_type', $params)) {
                $this->setWashMode($params['wash_type'] === null ? null : (string)$params['wash_type']);
            }
            if (array_key_exists('wash_mode', $params)) {
                $this->setWashMode($params['wash_mode'] === null ? null : (string)$params['wash_mode']);
            }
            if (array_key_exists('defer_relay_side_effects', $params)) {
                $this->setDeferRelaySideEffects(filter_var(
                    $params['defer_relay_side_effects'],
                    FILTER_VALIDATE_BOOLEAN
                ));
            }
        }
        return $this;
    }
}
