<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SalesProgram extends Model
{
    use HasFactory;

    protected $guarded = ['id'];

    // Casts mengubah tipe data secara otomatis saat ditarik/disimpan
    protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
        'target_customer_levels' => 'array', // Otomatis handle JSON ke Array
        'target_customer_types' => 'array',  // PERBAIKAN ENTERPRISE: Menangani mutasi Checklist Multiple ID dari/ke JSON Array
        'is_active' => 'boolean',
        'min_qty' => 'float',
        'min_amount' => 'float',
        'discount_percent' => 'float',
        'discount_nominal' => 'float',
        'free_qty' => 'float',
    ];

    // Relasi Kategori (Jika scope = category)
    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id');
    }

    // Relasi Produk Syarat (Jika scope = product)
    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id');
    }

    // Relasi Produk Bonus (Jika ada bonus barang)
    public function freeProduct()
    {
        return $this->belongsTo(Product::class, 'free_product_id');
    }

    // =========================================================
    // TAMBAHAN PENTING: Relasi ke Tiers (Jenjang Promo)
    // =========================================================
    public function tiers()
    {
        return $this->hasMany(SalesProgramTier::class, 'sales_program_id');
    }
}