<?php

namespace App\Modules\Pilkades\Services;

use App\Modules\Pilkades\Models\Desa;
use BaconQrCode\Renderer\Image\SvgImageBackEnd;
use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer;

class ShareService
{
    public function landingUrl(Desa $desa): string
    {
        $desa->loadMissing('kecamatan.kabupaten');

        return url("/{$desa->kecamatan->kabupaten->slug}/{$desa->kecamatan->slug}/{$desa->slug}");
    }

    /**
     * Renders the landing page URL as an SVG QR code — SVG output needs
     * neither gd nor imagick, so it works on any shared-hosting PHP build.
     */
    public function qrCodeSvg(Desa $desa): string
    {
        $renderer = new ImageRenderer(
            new RendererStyle(300),
            new SvgImageBackEnd,
        );

        return (new Writer($renderer))->writeString($this->landingUrl($desa));
    }

    public function whatsappShareUrl(Desa $desa): string
    {
        $text = "Informasi Pilkades Desa {$desa->nama}: {$this->landingUrl($desa)}";

        return 'https://wa.me/?text='.urlencode($text);
    }
}
