<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class AcademyPromptPack extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'title',
        'slug',
        'excerpt',
        'description',
        'access_level',
        'one_time_price_cents',
        'currency',
        'cover_image',
        'tags',
        'featured',
        'active',
        'published_at',
    ];

    protected $casts = [
        'one_time_price_cents' => 'integer',
        'tags' => 'array',
        'featured' => 'boolean',
        'active' => 'boolean',
        'published_at' => 'datetime',
    ];

    public function scopeActive(Builder $query): Builder
    {
        return $query->where('active', true);
    }

    public function scopePublished(Builder $query): Builder
    {
        return $query->whereNotNull('published_at')->where('published_at', '<=', now());
    }

    public function prompts(): BelongsToMany
    {
        return $this->belongsToMany(AcademyPromptTemplate::class, 'academy_prompt_pack_items', 'pack_id', 'prompt_template_id')
            ->withPivot('order_num')
            ->withTimestamps()
            ->orderBy('academy_prompt_pack_items.order_num');
    }
}