<?php

namespace objects;

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

class selfserve_wash_session_answers_o extends db
{
    use db_object_t;

    public object_property $session_id;
    public object_property $question_id;
    public object_property $question_text;
    public object_property $answer_value;
    public object_property $answered_at;
    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_answers');
    }

    public function getObjectProperties(): void
    {
        $this->session_id = new object_property($this->table, $this->id, 'session_id', 'int', false);
        $this->question_id = new object_property($this->table, $this->id, 'question_id', 'int', false);
        $this->question_text = new object_property($this->table, $this->id, 'question_text', 'string', false);
        $this->answer_value = new object_property($this->table, $this->id, 'answer_value', 'bool', false);
        $this->answered_at = new object_property($this->table, $this->id, 'answered_at', 'datetime', 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 upsert(int $sessionId, int $questionId, string $questionText, bool $answerValue): self
    {
        $rows = $this->getFieldsWhere([
            'session_id' => $sessionId,
            'question_id' => $questionId,
            'deleted_at' => null,
        ], ['id']);

        if ($rows !== []) {
            $this->select((int)$rows[0]['id']);
            $this->question_text->set($questionText);
            $this->answer_value->set($answerValue);
            $this->answered_at->set(date('Y-m-d H:i:s'));
            return $this;
        }

        $this->id = self::add_object([
            'session_id' => $sessionId,
            'question_id' => $questionId,
            'question_text' => $questionText,
            'answer_value' => $answerValue,
            'answered_at' => date('Y-m-d H:i:s'),
        ]);
        $this->getObjectProperties();
        $this->objectChanged();
        return $this;
    }

    public function listBySession(int $sessionId): array
    {
        return $this->getFieldsWhere([
            'session_id' => $sessionId,
            'deleted_at' => null,
        ], ['id', 'question_id', 'question_text', 'answer_value', 'answered_at']);
    }

    public function deleteMissingForSession(int $sessionId, array $questionIds): void
    {
        global $db;

        $sessionId = (int)$sessionId;
        $questionIds = array_values(array_unique(array_map(static fn(mixed $value): int => (int)$value, $questionIds)));

        if ($questionIds === []) {
            $db->query("DELETE FROM $this->table WHERE session_id = $sessionId");
            return;
        }

        $questionIdsSql = implode(',', $questionIds);
        $db->query("DELETE FROM $this->table WHERE session_id = $sessionId AND question_id NOT IN ($questionIdsSql)");
    }
}
