<?php

namespace attachments\helpers;

use objects\object_attachments_o;

class attachment
{
    public ?int $id;
    public ?string $object_type;
    public ?int $object_id;
    public attachment_content $content;
    public ?string $created_at;
    public ?string $updated_at;
    public ?string $deleted_at;

    public function __construct(?object_attachments_o $object_attachments_o = null)
    {
        // If no object is provided, initialize with default values
        if ($object_attachments_o) {
            $decodedContent = (object)json_decode($object_attachments_o->content->value(), true);
            $this->populate((object)[
                // Convert object_attachments_o properties to the expected types
                'id' => $object_attachments_o->id,
                'object_type' => $object_attachments_o->object_type->value(),
                'object_id' => $object_attachments_o->object_id->value(),
                'content' => $decodedContent,
                'created_at' => $object_attachments_o->created_at,
                'updated_at' => $object_attachments_o->updated_at,
                'deleted_at' => $object_attachments_o->deleted_at,
            ]);
        }
    }
    public function populate(object $object): self
    {
        // If the object is provided, populate the properties
        if ($object) {
            $this->id = isset($object->id) ? (int)$object->id : null;
            $this->object_type = isset($object->object_type) ? (string)$object->object_type : null;
            $this->object_id = isset($object->object_id) ? (int)$object->object_id : null;
            $this->content = isset($object->content) ? new attachment_content((object)$object->content) : null;
            $this->created_at = isset($object->created_at) ? (string)$object->created_at : null;
            $this->updated_at = isset($object->updated_at) ? (string)$object->updated_at : null;
            $this->deleted_at = isset($object->deleted_at) ? (string)$object->deleted_at : null;
        }
        return $this;
    }

    public function isWashCertificate(): bool
    {
        return isset($this->content->other) && $this->content->other === 'wash_certificate';
    }
}