<?php

declare(strict_types=1);

namespace App\Modules\Support\Domain;

class SupportRequestMessage
{
    public function __construct(
        public readonly string $id,
        public readonly string $supportRequestId,
        public readonly string $tenantId,
        public readonly string $authorName,
        public readonly string $authorRole,
        public readonly string $messageKind,
        public readonly string $body,
        public readonly string $createdAt,
        public readonly ?string $authorUserId = null,
        public readonly ?string $authorTenantUserId = null,
        public readonly ?string $newStatus = null,
    ) {
    }

    /**
     * @param array<string, mixed> $row
     */
    public static function fromRow(array $row): self
    {
        return new self(
            (string) ($row['id'] ?? ''),
            (string) ($row['support_request_id'] ?? ''),
            (string) ($row['tenant_id'] ?? ''),
            (string) ($row['author_name'] ?? ''),
            (string) ($row['author_role'] ?? 'member'),
            (string) ($row['message_kind'] ?? 'public_reply'),
            (string) ($row['body'] ?? ''),
            (string) ($row['created_at'] ?? ''),
            isset($row['author_user_id']) ? (string) $row['author_user_id'] : null,
            isset($row['author_tenant_user_id']) ? (string) $row['author_tenant_user_id'] : null,
            isset($row['new_status']) && $row['new_status'] !== '' ? (string) $row['new_status'] : null,
        );
    }

    /**
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'support_request_id' => $this->supportRequestId,
            'tenant_id' => $this->tenantId,
            'author_name' => $this->authorName,
            'author_role' => $this->authorRole,
            'message_kind' => $this->messageKind,
            'body' => $this->body,
            'created_at' => $this->createdAt,
            'author_user_id' => $this->authorUserId,
            'author_tenant_user_id' => $this->authorTenantUserId,
            'new_status' => $this->newStatus,
        ];
    }
}
