<?php

declare(strict_types=1);

namespace App\Models;

use App\Models\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Builder;
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 $board_id
 * @property string|null $category_id
 * @property string|null $user_id
 * @property string|null $guest_name
 * @property string|null $guest_password_hash
 * @property string $title
 * @property string $content
 * @property string|null $excerpt
 * @property bool $is_notice
 * @property bool $is_secret
 * @property bool $is_html
 * @property string $status
 * @property int $view_count
 * @property int $like_count
 * @property int $comment_count
 * @property int $attachment_count
 * @property array|null $meta
 * @property string|null $ip
 * @property \Illuminate\Support\Carbon|null $published_at
 */
class Post extends Model
{
    use BelongsToTenant;
    use HasFactory;
    use HasUuids;
    use SoftDeletes;

    protected $fillable = [
        'tenant_id', 'board_id', 'category_id', 'user_id',
        'guest_name', 'guest_password_hash',
        'title', 'content', 'excerpt',
        'is_notice', 'is_secret', 'is_html', 'status',
        'view_count', 'like_count', 'comment_count', 'attachment_count',
        'meta', 'ip', 'published_at',
    ];

    protected $hidden = ['guest_password_hash'];

    protected function casts(): array
    {
        return [
            'is_notice' => 'boolean',
            'is_secret' => 'boolean',
            'is_html' => 'boolean',
            'view_count' => 'integer',
            'like_count' => 'integer',
            'comment_count' => 'integer',
            'attachment_count' => 'integer',
            'meta' => 'array',
            'published_at' => 'datetime',
        ];
    }

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

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

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

    public function comments(): HasMany
    {
        return $this->hasMany(PostComment::class)->orderBy('created_at');
    }

    public function attachments(): HasMany
    {
        return $this->hasMany(PostAttachment::class);
    }

    public function reactions(): HasMany
    {
        return $this->hasMany(PostReaction::class);
    }

    public function scopeNotice(Builder $q): Builder
    {
        return $q->where('is_notice', true);
    }

    public function scopePublished(Builder $q): Builder
    {
        return $q->where('status', 'published');
    }

    public function scopeSearch(Builder $q, ?string $term): Builder
    {
        if (! $term) return $q;
        $like = '%'.str_replace(['%', '_'], ['\\%', '\\_'], $term).'%';
        return $q->where(function ($qq) use ($like) {
            $qq->where('title', 'like', $like)
               ->orWhere('content', 'like', $like)
               ->orWhere('guest_name', 'like', $like);
        });
    }
}
