<?php

declare(strict_types=1);

namespace App\Models;

use App\Models\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * @property string $id
 * @property string $tenant_id
 * @property string $post_id
 * @property string|null $parent_id
 * @property string|null $user_id
 * @property string|null $guest_name
 * @property string|null $guest_password_hash
 * @property string $content
 * @property bool $is_secret
 * @property int $like_count
 * @property string|null $ip
 */
class PostComment extends Model
{
    use BelongsToTenant;
    use HasFactory;
    use HasUuids;
    use SoftDeletes;

    protected $fillable = [
        'tenant_id', 'post_id', 'parent_id', 'user_id',
        'guest_name', 'guest_password_hash',
        'content', 'is_secret', 'like_count', 'ip',
    ];

    protected $hidden = ['guest_password_hash'];

    protected function casts(): array
    {
        return [
            'is_secret' => 'boolean',
            'like_count' => 'integer',
        ];
    }

    public function post(): BelongsTo
    {
        return $this->belongsTo(Post::class);
    }

    public function author(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function parent(): BelongsTo
    {
        return $this->belongsTo(self::class, 'parent_id');
    }

    public function children(): HasMany
    {
        return $this->hasMany(self::class, 'parent_id')->orderBy('created_at');
    }
}
