<?php

namespace App\Models;

use App\Enums\KategoriJenis;
use Database\Factories\KategoriFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
 * @property int $id
 * @property string $nama_kategori
 * @property string|null $judul
 * @property string|null $deskripsi
 * @property string $alias
 * @property KategoriJenis $jenis
 * @property int $viewed
 * @property int $rotation_pointer
 * @property bool $status
 */
class Kategori extends Model
{
    /** @use HasFactory<KategoriFactory> */
    use HasFactory;

    protected $fillable = [
        'nama_kategori', 'judul', 'deskripsi', 'alias', 'jenis', 'viewed', 'rotation_pointer', 'status',
    ];

    protected function casts(): array
    {
        return [
            'jenis' => KategoriJenis::class,
            'viewed' => 'integer',
            'rotation_pointer' => 'integer',
            'status' => 'boolean',
        ];
    }

    public function ads(): BelongsToMany
    {
        return $this->belongsToMany(Ad::class, 'ad_kategori');
    }

    /**
     * Naikkan counter viewed (FR-KAT-03). Aman untuk konkurensi.
     */
    public function incrementViewed(): void
    {
        $this->increment('viewed');
    }
}
