<?php
// app/Models/Agency/AgencyPageSection.php

namespace App\Models\Agency;

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

class AgencyPageSection extends Model
{
    use HasFactory;

    protected $fillable = [
        'agency_page_id',
        'section_type',
        'section_key',
        'title',
        'subtitle',
        'content',
        'settings',
        'data',
        'sort_order',
        'is_active',
        'background_color',
        'background_image',
        'container_width',
        'padding_top',
        'padding_bottom',
        'show_on_mobile',
        'show_on_tablet',
        'show_on_desktop',
        'created_by',
        'updated_by',
    ];

    protected function casts(): array
    {
        return [
            'settings' => 'array',
            'data' => 'array',
            'is_active' => 'boolean',
            'show_on_mobile' => 'boolean',
            'show_on_tablet' => 'boolean',
            'show_on_desktop' => 'boolean',
        ];
    }

    // Relationships
    public function agencyPage()
    {
        return $this->belongsTo(AgencyPage::class);
    }

    public function creator()
    {
        return $this->belongsTo(\App\Models\User::class, 'created_by');
    }

    public function updater()
    {
        return $this->belongsTo(\App\Models\User::class, 'updated_by');
    }

    // Scopes
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeOrdered($query)
    {
        return $query->orderBy('sort_order');
    }

    // Helper Methods
    public function getConfig($key = null, $default = null)
    {
        if (is_null($key)) {
            return $this->settings ?? [];
        }

        return data_get($this->settings, $key, $default);
    }

    public function setConfigValue($key, $value)
    {
        $settings = $this->settings ?? [];
        data_set($settings, $key, $value);
        $this->settings = $settings;
        return $this;
    }

    public function mergeConfig(array $newConfig)
    {
        $this->settings = array_merge($this->settings ?? [], $newConfig);
        return $this;
    }

    public function getData($key = null, $default = null)
    {
        if (is_null($key)) {
            return $this->data ?? [];
        }

        return data_get($this->data, $key, $default);
    }

    public function setData($key, $value)
    {
        $data = $this->data ?? [];
        data_set($data, $key, $value);
        $this->data = $data;
        $this->save();
        return $this;
    }

    public function duplicate()
    {
        $newSection = $this->replicate();
        $newSection->title = $this->title . ' (Kopya)';
        $newSection->section_key = null; // Reset unique key
        $newSection->created_by = auth()->id();
        $newSection->updated_by = null;
        $newSection->save();

        return $newSection;
    }

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($section) {
            if (auth()->check()) {
                $section->created_by = auth()->id();
            }

            // Set sort order if not provided
            if (is_null($section->sort_order)) {
                $maxOrder = static::where('agency_page_id', $section->agency_page_id)->max('sort_order');
                $section->sort_order = ($maxOrder ?? -1) + 1;
            }
        });

        static::updating(function ($section) {
            if (auth()->check()) {
                $section->updated_by = auth()->id();
            }
        });
    }
}
