"use client";

import { RootState } from "@/libs/redux/store";
import { AlertTriangle, Download, Smartphone } from "lucide-react";
import { useSelector } from "react-redux";

function formatBytes(bytes: number): string {
    if (bytes <= 0) return "";
    const units = ["B", "KB", "MB", "GB"];
    const exponent = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1);
    const value = bytes / Math.pow(1024, exponent);
    return `${value.toFixed(exponent === 0 ? 0 : 1)} ${units[exponent]}`;
}

export default function DownloadAppPage() {
    const config = useSelector((state: RootState) => state.config);
    const hasApk = !!config.apk_download_url;

    return (
        <div className="max-w-screen-md mx-auto px-4 py-12 sm:py-16">
            <div className="flex flex-col items-center text-center gap-3 mb-10">
                <span className="w-14 h-14 rounded-full bg-primary-500 flex items-center justify-center">
                    <Smartphone className="w-7 h-7 text-white" />
                </span>
                <h1 className="font-serif text-2xl sm:text-3xl font-bold text-kb-ink">
                    Official Page for Download KutuBuku
                </h1>
                <p className="text-sm text-kb-muted max-w-md">
                    HP-mu masih di bawah Android 9 (SDK 28)? Atau lagi nggak bisa download lewat Google Play Store? Tenang aja! Kamu tetap bisa install KutuBuku menggunakan APK resmi yang kami sediakan di halaman ini.
                    <br />
                    <br />
                    <strong>
                        Peringatan:
                    </strong>
                    <br />
                    Main Aman, Pakai APK Resmi!
                    <br />
                    <br />
                    APK yang tersedia di halaman ini adalah APK resmi KutuBuku.
                    <br />
                    <br />
                    Jangan download APK KutuBuku dari website, grup, atau pihak lain yang tidak resmi. Demi keamanan akun dan perangkatmu, KutuBuku tidak bertanggung jawab atas segala risiko yang timbul akibat penggunaan APK yang diperoleh dari sumber selain website resmi KutuBuku.
                    <br />
                    <br />
                    Kalau bukan dari sini, jangan di-install ya! 😉
                </p>
            </div>

            <div className="rounded-3xl border border-kb-line bg-kb-paper-alt p-6 sm:p-8 flex flex-col items-center gap-5 text-center">
                {hasApk ? (
                    <>
                        <div>
                            <p className="font-semibold text-kb-ink">KutuBuku.apk</p>
                            <p className="text-xs text-kb-muted mt-1">
                                {[
                                    config.apk_version ? `Versi ${config.apk_version}` : null,
                                    config.apk_size ? formatBytes(config.apk_size) : null,
                                ]
                                    .filter(Boolean)
                                    .join(" · ")}
                            </p>
                        </div>

                        <a
                            href={config.apk_download_url!}
                            download={config.apk_original_name || undefined}
                            className="inline-flex items-center gap-2 bg-primary-600 hover:brightness-110 text-white font-semibold text-sm px-6 py-3 rounded-full transition"
                        >
                            <Download className="w-4 h-4" />
                            Download APK
                        </a>

                        <p className="text-[11px] text-kb-muted max-w-sm">
                            Pastikan opsi &quot;Install dari sumber tidak dikenal&quot; diaktifkan
                            di pengaturan HP kamu sebelum menginstal file APK ini.
                        </p>
                    </>
                ) : (
                    <>
                        <AlertTriangle className="w-8 h-8 text-amber-500" />
                        <p className="text-sm text-kb-muted max-w-sm">
                            File APK belum tersedia saat ini. Silakan cek kembali beberapa
                            saat lagi, atau unduh lewat Google Play Store jika perangkatmu
                            mendukung.
                        </p>
                        {config.link_playstore && (
                            <a
                                href={config.link_playstore}
                                target="_blank"
                                rel="noopener noreferrer"
                                className="inline-flex items-center gap-2 border border-kb-line hover:bg-white text-kb-ink font-semibold text-sm px-6 py-3 rounded-full transition"
                            >
                                Buka Google Play Store
                            </a>
                        )}
                    </>
                )}
            </div>
        </div>
    );
}
