<?php

namespace App\Models;

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

class UserSetting extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'user_id',
        'cooldown_hours',
        'exclude_contacts',
        'sync_enabled',
        'user_sms_service_enabled',
        'reply_to_incoming',
        'reply_to_missed',
        'reply_to_outgoing',
        'promotion_on_incoming',
        'promotion_on_missed',
        'promotion_on_outgoing',
        // Channel preference
        'preferred_channel',
        // WhatsApp settings
        'whatsapp_enabled',
        'user_whatsapp_service_enabled',
        'whatsapp_reply_to_incoming',
        'whatsapp_reply_to_missed',
        'whatsapp_reply_to_outgoing',
        'whatsapp_promotion_on_incoming',
        'whatsapp_promotion_on_outgoing',
        'whatsapp_promotion_on_missed',
        'whatsapp_mode',
        'whatsapp_cooldown_hours',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'cooldown_hours' => 'integer',
        'exclude_contacts' => 'boolean',
        'sync_enabled' => 'boolean',
        'user_sms_service_enabled' => 'boolean',
        'reply_to_incoming' => 'boolean',
        'reply_to_missed' => 'boolean',
        'reply_to_outgoing' => 'boolean',
        'promotion_on_incoming' => 'boolean',
        'promotion_on_missed' => 'boolean',
        'promotion_on_outgoing' => 'boolean',
        // WhatsApp casts
        'whatsapp_enabled' => 'boolean',
        'user_whatsapp_service_enabled' => 'boolean',
        'whatsapp_reply_to_incoming' => 'boolean',
        'whatsapp_reply_to_missed' => 'boolean',
        'whatsapp_reply_to_outgoing' => 'boolean',
        'whatsapp_promotion_on_incoming' => 'boolean',
        'whatsapp_promotion_on_outgoing' => 'boolean',
        'whatsapp_promotion_on_missed' => 'boolean',
        'whatsapp_cooldown_hours' => 'integer',
    ];

    /**
     * Get the user that owns the settings.
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the default settings array.
     */
    public static function getDefaults(): array
    {
        return [
            'cooldown_hours' => 24,
            'exclude_contacts' => false,
            'sync_enabled' => true,
            'user_sms_service_enabled' => true,
            'reply_to_incoming' => true,
            'reply_to_missed' => true,
            'reply_to_outgoing' => false,
            'promotion_on_incoming' => false,
            'promotion_on_missed' => false,
            'promotion_on_outgoing' => false,
            // Channel preference
            'preferred_channel' => 'sms',
            // WhatsApp defaults
            'whatsapp_enabled' => false,
            'user_whatsapp_service_enabled' => true,
            'whatsapp_reply_to_incoming' => false,
            'whatsapp_reply_to_missed' => false,
            'whatsapp_reply_to_outgoing' => false,
            'whatsapp_promotion_on_incoming' => false,
            'whatsapp_promotion_on_outgoing' => false,
            'whatsapp_promotion_on_missed' => false,
            'whatsapp_mode' => 'manual',
            'whatsapp_cooldown_hours' => 24,
        ];
    }
}
