<?php

declare(strict_types=1);

namespace App\Modules\Support\Domain;

class SupportRequest
{
    /**
     * @param array<int, SupportRequestMessage> $messages
     */
    public function __construct(
        public readonly string $id,
        public readonly string $tenantId,
        public readonly string $requestNumber,
        public readonly string $requestDirection,
        public readonly string $routeTarget,
        public readonly string $category,
        public readonly string $priority,
        public readonly string $status,
        public readonly string $subject,
        public readonly string $message,
        public readonly string $requesterName,
        public readonly string $requesterEmail,
        public readonly string $lastMessageAt,
        public readonly string $createdAt,
        public readonly string $updatedAt,
        public readonly ?string $requesterUserId = null,
        public readonly ?string $requesterTenantUserId = null,
        public readonly ?string $assignedToUserId = null,
        public readonly ?string $assignedToTenantUserId = null,
        public readonly ?string $resolvedAt = null,
        public readonly ?string $closedAt = null,
        public readonly array $messages = [],
    ) {
    }

    /**
     * @param array<string, mixed> $row
     * @param array<int, SupportRequestMessage> $messages
     */
    public static function fromRow(array $row, array $messages = []): self
    {
        return new self(
            (string) ($row['id'] ?? ''),
            (string) ($row['tenant_id'] ?? ''),
            (string) ($row['request_number'] ?? ''),
            (string) ($row['request_direction'] ?? 'member_to_tenant'),
            (string) ($row['route_target'] ?? 'tenant_responsible'),
            (string) ($row['category'] ?? 'general'),
            (string) ($row['priority'] ?? 'normal'),
            (string) ($row['status'] ?? 'new'),
            (string) ($row['subject'] ?? ''),
            (string) ($row['message'] ?? ''),
            (string) ($row['requester_name'] ?? ''),
            (string) ($row['requester_email'] ?? ''),
            (string) ($row['last_message_at'] ?? ''),
            (string) ($row['created_at'] ?? ''),
            (string) ($row['updated_at'] ?? ''),
            isset($row['requester_user_id']) ? (string) $row['requester_user_id'] : null,
            isset($row['requester_tenant_user_id']) ? (string) $row['requester_tenant_user_id'] : null,
            isset($row['assigned_to_user_id']) ? (string) $row['assigned_to_user_id'] : null,
            isset($row['assigned_to_tenant_user_id']) ? (string) $row['assigned_to_tenant_user_id'] : null,
            isset($row['resolved_at']) && $row['resolved_at'] !== '' ? (string) $row['resolved_at'] : null,
            isset($row['closed_at']) && $row['closed_at'] !== '' ? (string) $row['closed_at'] : null,
            $messages,
        );
    }

    /**
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'tenant_id' => $this->tenantId,
            'request_number' => $this->requestNumber,
            'request_direction' => $this->requestDirection,
            'route_target' => $this->routeTarget,
            'category' => $this->category,
            'priority' => $this->priority,
            'status' => $this->status,
            'subject' => $this->subject,
            'message' => $this->message,
            'requester_name' => $this->requesterName,
            'requester_email' => $this->requesterEmail,
            'last_message_at' => $this->lastMessageAt,
            'created_at' => $this->createdAt,
            'updated_at' => $this->updatedAt,
            'requester_user_id' => $this->requesterUserId,
            'requester_tenant_user_id' => $this->requesterTenantUserId,
            'assigned_to_user_id' => $this->assignedToUserId,
            'assigned_to_tenant_user_id' => $this->assignedToTenantUserId,
            'resolved_at' => $this->resolvedAt,
            'closed_at' => $this->closedAt,
            'messages' => array_map(
                static fn (SupportRequestMessage $message): array => $message->toArray(),
                $this->messages
            ),
        ];
    }
}
