<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Cache;

class Ad extends Model
{
    protected $fillable = ['name', 'description', 'position', 'size', 'code', 'is_active', 'sort_order'];

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

    /**
     * Clear cached ads when any ad is saved or deleted
     */
    protected static function booted(): void
    {
        static::saved(function () {
            Cache::forget('ad_popunder');
            Cache::forget('ad_social_bar');
        });
        static::deleted(function () {
            Cache::forget('ad_popunder');
            Cache::forget('ad_social_bar');
        });
    }

    /**
     * Available positions with labels
     */
    public static function positions(): array
    {
        return [
            'banner_top'    => 'Banner Atas (/, /dashboard, /lowongan/{job})',
            'banner_bottom' => 'Banner Bawah (/, /lowongan/{job}, /apply/{job}/ads, /form)',
            'sidebar'       => 'Sidebar (/lowongan/{job})',
            'native'        => 'Native / Sisipan (/, /lowongan/{job}, /dashboard, /form)',
            'in_article'    => 'Dalam Artikel (/lowongan/{job}, /dashboard)',
            'countdown'     => 'Countdown (/apply/{job}/ads, /form)',
            'interstitial'  => 'Interstitial (/apply/{job}/ads)',
            'mobile_banner' => 'Banner Mobile (/, /lowongan/{job}, /apply/{job}/ads, /dashboard, /form)',
            'popunder'      => 'Smartlink (kirim formulir tahap 3, 4 & 5)',
            'social_bar'    => 'Social Bar / Floating (semua halaman)',
        ];
    }

    /**
     * Available banner sizes
     */
    public static function sizes(): array
    {
        return [
            '728x90'  => 'Leaderboard (728x90)',
            '468x60'  => 'Banner (468x60)',
            '320x50'  => 'Mobile Banner (320x50)',
            '300x250' => 'Medium Rectangle (300x250)',
            '160x600' => 'Wide Skyscraper (160x600)',
            '160x300' => 'Half-Page (160x300)',
        ];
    }

    /**
     * Get single active ad by position
     */
    public static function getByPosition(string $position): ?self
    {
        return static::where('position', $position)
            ->where('is_active', true)
            ->orderBy('sort_order')
            ->first();
    }

    /**
     * Get all active ads by position
     */
    public static function getAllByPosition(string $position): Collection
    {
        return static::where('position', $position)
            ->where('is_active', true)
            ->orderBy('sort_order')
            ->get();
    }

    /**
     * Get active ad by position and size
     */
    public static function getByPositionAndSize(string $position, string $size): ?self
    {
        return static::where('position', $position)
            ->where('size', $size)
            ->where('is_active', true)
            ->first();
    }

    /**
     * Get position label
     */
    public function getPositionLabelAttribute(): string
    {
        return static::positions()[$this->position] ?? ucfirst(str_replace('_', ' ', $this->position));
    }

    /**
     * Get size label
     */
    public function getSizeLabelAttribute(): string
    {
        return static::sizes()[$this->size] ?? ($this->size ?: '-');
    }
}
