<?php

namespace App\Models\Boards;

use App\Models\Tenants\Tenant;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 게시판 모델 (mng용)
 *
 * @property int $id
 * @property int|null $tenant_id
 * @property bool $is_system 시스템 게시판 여부
 * @property string|null $board_type 게시판 유형
 * @property string $board_code 게시판 코드
 * @property string $name 게시판명
 * @property string|null $description 설명
 * @property string $editor_type 에디터 타입
 * @property bool $allow_files 파일 첨부 허용
 * @property int $max_file_count 최대 파일 수
 * @property int $max_file_size 최대 파일 크기 (KB)
 * @property array|null $extra_settings 추가 설정
 * @property bool $is_active 활성 여부
 */
class Board extends Model
{
    use SoftDeletes;

    protected $table = 'boards';

    protected $fillable = [
        'tenant_id',
        'is_system',
        'board_type',
        'board_code',
        'name',
        'description',
        'editor_type',
        'allow_files',
        'max_file_count',
        'max_file_size',
        'extra_settings',
        'is_active',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    protected $casts = [
        'is_system' => 'boolean',
        'is_active' => 'boolean',
        'allow_files' => 'boolean',
        'extra_settings' => 'array',
    ];

    protected $attributes = [
        'is_system' => false,
        'is_active' => true,
        'allow_files' => true,
        'max_file_count' => 5,
        'max_file_size' => 20480,
        'editor_type' => 'wysiwyg',
    ];

    // =========================================================================
    // Scopes
    // =========================================================================

    /**
     * 시스템 게시판만 (mng용)
     */
    public function scopeSystemOnly(Builder $query): Builder
    {
        return $query->where('is_system', true);
    }

    /**
     * 활성 게시판만
     */
    public function scopeActive(Builder $query): Builder
    {
        return $query->where('is_active', true);
    }

    /**
     * 게시판 유형으로 필터링
     */
    public function scopeOfType(Builder $query, string $type): Builder
    {
        return $query->where('board_type', $type);
    }

    // =========================================================================
    // Relationships
    // =========================================================================

    public function fields(): HasMany
    {
        return $this->hasMany(BoardSetting::class, 'board_id')
            ->orderBy('sort_order');
    }

    public function posts(): HasMany
    {
        return $this->hasMany(Post::class, 'board_id');
    }

    public function tenant(): BelongsTo
    {
        return $this->belongsTo(Tenant::class, 'tenant_id');
    }

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

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

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

    // =========================================================================
    // Helper Methods
    // =========================================================================

    /**
     * extra_settings에서 특정 키 값 가져오기
     */
    public function getSetting(string $key, $default = null)
    {
        return data_get($this->extra_settings, $key, $default);
    }

    /**
     * 시스템 게시판 여부 확인
     */
    public function isSystemBoard(): bool
    {
        return $this->is_system === true;
    }
}
