/**
 * helpers.ts — Shared utility functions for Oikka Capital
 *
 * Import what you need:
 *   import { formatDate, formatCurrency, getInitials } from '@/helpers';
 *   import { useAuth } from '@/helpers';
 */

import { computed } from 'vue';
import { usePage } from '@inertiajs/vue3';
import { PageProps, User as AuthUser } from '@/types';

// ─────────────────────────────────────────────────────────────────────────────
// Date & Currency Formatting
// ─────────────────────────────────────────────────────────────────────────────

/**
 * Format an ISO date string into a human-readable date (e.g. "Jul 14, 2026").
 */
export const formatDate = (dateString: string): string =>
    new Date(dateString).toLocaleDateString('en-US', {
        year: 'numeric',
        month: 'short',
        day: 'numeric',
    });

/**
 * Format a numeric amount as a Taka currency string.
 * @example formatCurrency(5000) => "Tk 5,000.00"
 */
export const formatCurrency = (amount: number | string, prefix = 'Tk '): string => {
    const num = typeof amount === 'string' ? parseFloat(amount) : amount;
    return prefix + (isNaN(num) ? '0.00' : num.toLocaleString('en-US', {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2,
    }));
};

/**
 * Extract up to 2 uppercase initials from a full name.
 * @example getInitials("Mehedi Hasan") => "MH"
 */
export const getInitials = (name: string): string =>
    name.split(' ').map(n => n[0]).slice(0, 2).join('').toUpperCase();

// ─────────────────────────────────────────────────────────────────────────────
// Navigation Helpers
// ─────────────────────────────────────────────────────────────────────────────

/**
 * Returns Tailwind classes for a sidebar nav link depending on whether it
 * matches the current URL prefix.
 */
export const navLinkClass = (prefix: string, currentUrl: string): string => {
    const isActive = prefix === '/'
        ? currentUrl === prefix
        : currentUrl.startsWith(prefix);

    return isActive
        ? 'bg-indigo-50 text-indigo-700 dark:bg-indigo-950/50 dark:text-indigo-400 font-semibold'
        : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900 dark:text-gray-300 dark:hover:bg-gray-700/50 dark:hover:text-white';
};

/**
 * Returns Tailwind classes for a sidebar nav link icon.
 */
export const navIconClass = (prefix: string, currentUrl: string): string => {
    const isActive = prefix === '/'
        ? currentUrl === prefix
        : currentUrl.startsWith(prefix);

    return isActive
        ? 'text-indigo-600 dark:text-indigo-400'
        : 'text-gray-400 group-hover:text-gray-500 dark:text-gray-400 dark:group-hover:text-gray-300';
};

// ─────────────────────────────────────────────────────────────────────────────
// Auth Composable
// ─────────────────────────────────────────────────────────────────────────────

export const useAuth = () => {
    const page = usePage<PageProps>();

    const user = computed<AuthUser>(() => page.props.auth.user);

    const isSuperAdmin = computed(() =>
        user.value.roles.includes('super_admin')
    );

    /** Mirrors the backend hasRole() — super_admin passes any role check. */
    const hasRole = (role: string): boolean =>
        isSuperAdmin.value || user.value.roles.includes(role);

    const isAdmin = computed(() => hasRole('admin'));
    const isMember = computed(() => hasRole('member'));
    const isFounder = computed(() => hasRole('founder'));

    /** True if the user is admin or super_admin (most common check). */
    const isPrivileged = computed(() =>
        isSuperAdmin.value || user.value.roles.includes('admin')
    );

    /** Check if the user has a specific permission. super_admin bypasses all checks. */
    const hasPermission = (permission: string): boolean =>
        isSuperAdmin.value || (user.value.permissions || []).includes(permission);

    return { user, isSuperAdmin, isAdmin, isMember, isFounder, isPrivileged, hasRole, hasPermission };
};
