<?php

namespace App\Models;

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

class SiteSetting extends Model
{
    use HasFactory;

    protected $table = 'site_settings';
    
    protected $fillable = [
        'key',
        'value'
    ];

    /**
     * Get setting value by key
     */
    public static function get($key, $default = null)
    {
        $setting = self::where('key', $key)->first();
        return $setting ? $setting->value : $default;
    }

    /**
     * Set setting value
     */
    public static function set($key, $value)
    {
        $setting = self::where('key', $key)->first();
        
        if ($setting) {
            $setting->update(['value' => $value]);
        } else {
            $setting = self::create([
                'key' => $key,
                'value' => $value
            ]);
        }
        
        return $setting;
    }

    /**
     * Get all settings as array
     */
    public static function getAll()
    {
        return self::all()->pluck('value', 'key')->toArray();
    }

    /**
     * Get settings by category
     */
    public static function getByCategory($category)
    {
        $categories = [
            'company' => [
                'company_name',
                'company_logo', 
                'company_address',
                'company_phone',
                'company_email',
                'working_hours',
                'whatsapp_number'
            ],
            'social' => [
                'facebook_url',
                'twitter_url',
                'linkedin_url',
                'instagram_url'
            ],
            'stats' => [
                'total_clients',
                'revenue_generated'
            ],
            'footer' => [
                'footer_text',
                'copyright_text'
            ],
            'maps' => [
                'google_map_embed'
            ],
            'seo' => [
                'meta_title',
                'meta_description',
                'meta_keywords'
            ]
        ];
        
        if (isset($categories[$category])) {
            $settings = [];
            foreach ($categories[$category] as $key) {
                $settings[$key] = self::get($key);
            }
            return $settings;
        }
        
        return [];
    }
}