<?php

namespace App\DTOs;

final readonly class CommentData
{
    public function __construct(
        public string $commentableType,
        public int $commentableId,
        public string $authorName,
        public string $content,
        public ?int $rating,
        public bool $isApproved = false,
        public ?string $adminReply = null,
    ) {}

    /**
     * @param  array<string, mixed>  $data
     */
    public static function fromArray(array $data): self
    {
        return new self(
            commentableType: $data['commentable_type'],
            commentableId: $data['commentable_id'],
            content: $data['content'],
            authorName: $data['author_name'],
            rating: $data['rating'] ?? null,
            isApproved: $data['is_approved'] ?? false,
            adminReply: $data['admin_reply'] ?? null,
        );
    }

    /**
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'commentable_type' => $this->commentableType,
            'commentable_id' => $this->commentableId,
            'author_name' => $this->authorName,
            'content' => $this->content,
            'rating' => $this->rating,
            'is_approved' => $this->isApproved,
            'admin_reply' => $this->adminReply,
        ];
    }
}
