<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    protected $fillable = [
        'nama_kantor', 'alamat', 'telepon', 'whatsapp',
        'email', 'jam_pelayanan', 'latitude', 'longitude',
    ];

    protected $casts = [
        'latitude'  => 'float',
        'longitude' => 'float',
    ];

    /**
     * Ambil data kontak (singleton).
     */
    public static function current(): self
    {
        return static::firstOrCreate([], [
            'nama_kantor' => 'Kantor Kepala Desa Suka Baru',
            'alamat'      => 'Desa Suka Baru, Kec. Marga Sakti Sebelat, Bengkulu Utara',
            'jam_pelayanan' => 'Senin – Jumat, 08.00 – 15.00 WIB',
            'latitude'    => -3.85,
            'longitude'   => 101.90,
        ]);
    }

    public function getWhatsappUrlAttribute(): ?string
    {
        if (!$this->whatsapp) return null;
        $number = preg_replace('/[^0-9]/', '', $this->whatsapp);
        if (str_starts_with($number, '0')) {
            $number = '62' . substr($number, 1);
        }
        return 'https://wa.me/' . $number;
    }

    public function getMapsUrlAttribute(): ?string
    {
        if (!$this->latitude || !$this->longitude) return null;
        return "https://www.google.com/maps?q={$this->latitude},{$this->longitude}";
    }
}
