<?php

namespace App\Models\Solicitor;

use App\Casts\EmailAddress;
use App\Casts\PhoneNumber;
use App\Interface\AddressOwner;
use App\Interface\DocumentOwner;
use App\Interface\HistoryOwner;
use App\Interface\NoteOwner;
use App\Models\User\User;
use App\Trait\HasAddresses;
use App\Trait\HasDocuments;
use App\Trait\HasHistory;
use App\Trait\HasNotes;
use Illuminate\Database\Eloquent\Model;

class Solicitor extends Model implements AddressOwner, DocumentOwner, NoteOwner, HistoryOwner {
    protected $fillable = [
        'name',
        'domain',
        'email_address',
        'phone_number',
        'logo_path',
        'company_number',
        'sra_number',
        'signature',
        'signature_text',
    ];

    protected $casts = [
        'email_address' => EmailAddress::class,
        'phone_number' => PhoneNumber::class,
    ];

    use HasAddresses, HasNotes, HasDocuments, HasHistory;


    public function users() {
        return $this->hasMany(User::class);
    }

    public function emailSenders() {
        return $this->hasMany(SolicitorEmailSender::class);
    }

    public function smsSenders() {
        return $this->hasMany(SolicitorSmsSender::class);
    }

    /**
     * Return the preferred sending address for a given purpose (e.g. 'dsar', 'client').
     * Prefers an explicit default for that purpose, then any sender for that purpose,
     * then falls back to the general default, then any sender at all.
     */
    public function emailSenderFor(string $purpose): ?SolicitorEmailSender {
        return $this->emailSenders()->where('purpose', $purpose)->orderByDesc('is_default')->first()
            ?? $this->emailSenders()->where('purpose', 'general')->orderByDesc('is_default')->first()
            ?? $this->emailSenders()->orderByDesc('is_default')->first();
    }

    /**
     * Firm-wide signature (HTML).  Used as the fallback when a SolicitorEmailSender
     * has no signature of its own.  Returns null if neither is set.
     */
    public function signatureHtml(): ?string
    {
        return $this->signature ?: null;
    }

    /**
     * Firm-wide signature (plain text).  Falls back to a stripped form of the HTML
     * signature when no explicit text version has been authored.
     */
    public function signatureText(): ?string
    {
        if ($this->signature_text) {
            return $this->signature_text;
        }
        if (!$this->signature) {
            return null;
        }
        $html = preg_replace('/<br\s*\/?>/i', "\n", $this->signature);
        $html = preg_replace('/<\/p>/i', "\n\n", $html);
        return trim(strip_tags($html));
    }

    public function getDocumentStoragePath(): string {
        return "solicitors/" . $this->id . "/";
    }
}
