<?php

namespace App\Models;

use App\Traits\Auditable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class Setting extends Model
{
    use HasFactory, Auditable;

    protected $fillable = [
        'site_name',
        'tagline',
        'logo',
        'logo_media_id',
        'favicon',
        'favicon_media_id',
        'contact_email',
        'contact_phone',
        'smtp_host',
        'smtp_port',
        'smtp_username',
        'smtp_password',
        'smtp_scheme',
        'smtp_from_address',
        'smtp_from_name',
        'footer_text',
        'social_links',
        'header_cta_label',
        'header_cta_url',
        'footer_cta_label',
        'footer_cta_url',
        'admin_contact_notifications_enabled',
        'user_contact_auto_reply_enabled',
        'support_chat_enabled',
        'support_chat_title',
        'support_chat_intro',
        'support_chat_button_label',
        'admin_contact_subject',
        'admin_contact_template',
        'user_contact_subject',
        'user_contact_template',
        'gtm_container_id',
        'ga_measurement_id',
    ];

    protected $casts = [
        'social_links' => 'array',
        'smtp_port' => 'integer',
        'admin_contact_notifications_enabled' => 'boolean',
        'user_contact_auto_reply_enabled' => 'boolean',
        'support_chat_enabled' => 'boolean',
    ];

    public function mediaUsages(): MorphMany
    {
        return $this->morphMany(MediaUsage::class, 'usable');
    }

    public function logoMedia(): BelongsTo
    {
        return $this->belongsTo(Media::class, 'logo_media_id');
    }

    public function faviconMedia(): BelongsTo
    {
        return $this->belongsTo(Media::class, 'favicon_media_id');
    }

    public function getLogoUrlAttribute(): string
    {
        $media = $this->relationLoaded('logoMedia') ? $this->logoMedia : $this->logoMedia()->first();

        if ($media) {
            return $media->url;
        }

        return asset($this->logo ?: 'uploads/placeholders/default-logo.png');
    }

    public function getFaviconUrlAttribute(): string
    {
        $media = $this->relationLoaded('faviconMedia') ? $this->faviconMedia : $this->faviconMedia()->first();

        if ($media) {
            return $media->url;
        }

        return asset($this->favicon ?: 'uploads/placeholders/saleem-sial-favicon.png');
    }

    public function getWhatsappUrlAttribute(): ?string
    {
        if (blank($this->contact_phone)) {
            return null;
        }

        $digits = preg_replace('/\D+/', '', (string) $this->contact_phone);

        if (blank($digits)) {
            return null;
        }

        return 'https://wa.me/' . $digits;
    }

    public function usesCustomSmtp(): bool
    {
        return filled($this->smtp_host);
    }

    public function applyMailConfiguration(): void
    {
        if (! $this->usesCustomSmtp()) {
            return;
        }

        config([
            'mail.default' => 'smtp',
            'mail.mailers.smtp.scheme' => filled($this->smtp_scheme) ? $this->smtp_scheme : null,
            'mail.mailers.smtp.host' => $this->smtp_host,
            'mail.mailers.smtp.port' => $this->smtp_port ?: config('mail.mailers.smtp.port'),
            'mail.mailers.smtp.username' => $this->smtp_username,
            'mail.mailers.smtp.password' => $this->smtp_password,
            'mail.from.address' => $this->smtp_from_address ?: $this->contact_email ?: config('mail.from.address'),
            'mail.from.name' => $this->smtp_from_name ?: $this->site_name ?: config('mail.from.name'),
        ]);

        app('mail.manager')->forgetMailers();
    }
}
