<?php

namespace objects;

use classes\db;
use classes\object_property;
use classes\selfserve_schema_bootstrap;
use traits\db_object_t;

class selfserve_wash_session_tasks_o extends db
{
    use db_object_t;

    public object_property $session_id;
    public object_property $task_id;
    public object_property $task_text;
    public object_property $description;
    public object_property $services;
    public object_property $buttons;
    public object_property $dynamic_images_vehicle_type;
    public object_property $created_at;
    public object_property $updated_at;
    public object_property $deleted_at;

    public function structure(): void
    {
        selfserve_schema_bootstrap::ensureTables();
        $this->setTable('selfserve_wash_session_tasks');
    }

    public function getObjectProperties(): void
    {
        $this->session_id = new object_property($this->table, $this->id, 'session_id', 'int', false);
        $this->task_id = new object_property($this->table, $this->id, 'task_id', 'int', false);
        $this->task_text = new object_property($this->table, $this->id, 'task_text', 'string', false);
        $this->description = new object_property($this->table, $this->id, 'description', 'string', false);
        $this->services = new object_property($this->table, $this->id, 'services', 'json', false);
        $this->buttons = new object_property($this->table, $this->id, 'buttons', 'json', false);
        $this->dynamic_images_vehicle_type = new object_property($this->table, $this->id, 'dynamic_images_vehicle_type', 'string', false);
        $this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', false);
        $this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'timestamp', false);
        $this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
    }

    public function objectChanged(): void
    {
        // No dedicated cache invalidation yet.
    }

    public function addSnapshot(
        int $sessionId,
        ?int $taskId,
        string $taskText,
        ?string $description = null,
        ?array $services = null,
        ?array $buttons = null,
        ?int $thumb_position = null,
    ): self {
        $this->id = self::add_object([
            'session_id' => $sessionId,
            'task_id' => $taskId,
            'task_text' => $taskText,
            'description' => $description,
            'services' => $services,
            'buttons' => $buttons,
            'dynamic_images_vehicle_type' => $thumb_position === null ? null : (int)$thumb_position, // The rotations to do on the image.
        ]);
        $this->getObjectProperties();
        $this->objectChanged();

        return $this;
    }

    public function deleteBySession(int $sessionId): void
    {
        global $db;
        $db->query("DELETE FROM $this->table WHERE session_id = " . (int)$sessionId);
    }

    public function listBySession(int $sessionId): array
    {
        return $this->getFieldsWhere([
            'session_id' => $sessionId,
            'deleted_at' => null,
        ], ['id', 'task_id', 'task_text', 'description', 'services', 'buttons', 'dynamic_images_vehicle_type']);
    }
}
