/**
 * SpecialistIdentity — Reusable specialist profile header.
 *
 * Renders avatar with initials, verified badge, company name, category,
 * star rating, review count, and city/state location.
 *
 * Props:
 *   specialist (object) — Specialist JSON record with Company Name, Category, City, State
 */

/**
 * SpecialistRating — Deterministic pseudo-rating/review-count hashes derived
 * from a specialist's company name. Shared by SpecialistIdentity and
 * SpecialistDirectoryPage so both render identical values for the same name.
 */
var SpecialistRating = {
    getRating: function (n) {
        var hash = 0;
        for (var i = 0; i < (n || '').length; i++) {
            hash = ((hash << 5) - hash) + n.charCodeAt(i);
            hash = hash & hash;
        }
        return (3.5 + (Math.abs(hash) % 15) / 10).toFixed(1);
    },

    getReviewCount: function (n) {
        var hash = 0;
        for (var i = 0; i < (n || '').length; i++) {
            hash = ((hash << 3) - hash) + n.charCodeAt(i);
            hash = hash & hash;
        }
        return 8 + (Math.abs(hash) % 200);
    },
};

var SpecialistIdentity = function (props) {
    var specialist = props.specialist;
    if (!specialist) return null;

    var name = specialist['Company Name'] || '';
    var icon = (AppConstants.SPECIALIST_CATEGORY_ICONS || {})[specialist.Category] || 'support_agent';
    var rating = SpecialistRating.getRating(name);
    var reviews = SpecialistRating.getReviewCount(name);

    return (
        <section className="flex flex-col items-center py-6 mt-2 bg-gradient-to-b from-primary/5 to-transparent rounded-2xl">
            <div className="relative">
                <div className="w-24 h-24 rounded-full bg-primary/40 border-4 border-surface shadow-soft flex items-center justify-center">
                    <span
                        className="material-symbols-outlined text-white text-4xl"
                        style={{ fontVariationSettings: "'FILL' 1" }}
                    >{icon}</span>
                </div>
                {parseFloat(rating) >= 4.5 && (
                    <div className="absolute bottom-0 right-0 bg-primary text-white p-1 rounded-full border-2 border-surface">
                        <span className="material-symbols-outlined text-sm" style={{ fontVariationSettings: "'FILL' 1" }}>verified</span>
                    </div>
                )}
            </div>
            <div className="mt-3 text-center px-4">
                <h1 className="text-xl font-black text-forest tracking-tight">{name}</h1>
                <p className="text-forest font-bold text-sm mt-1">{specialist.Category}</p>
                <div className="flex items-center justify-center gap-1 mt-2">
                    <span className="material-symbols-outlined text-terracotta text-base" style={{ fontVariationSettings: "'FILL' 1" }}>star</span>
                    <span className="font-bold text-forest text-sm">{rating}</span>
                    <span className="text-forest text-xs">({reviews} reviews)</span>
                </div>
                {specialist.City && (
                    <div className="flex items-center justify-center gap-1 mt-1 text-muted">
                        <span className="material-symbols-outlined text-sm text-forest">location_on</span>
                        <span className="text-xs font-medium text-forest">{specialist.City}{specialist.State ? ', ' + specialist.State : ''}</span>
                    </div>
                )}
            </div>
        </section>
    );
};

window.SpecialistIdentity = SpecialistIdentity;
window.SpecialistRating = SpecialistRating;

/**
 * SpecialistService — Promise-cached loader for the specialists JSON.
 * Multiple pages (directory, profile, contact) all need the same dataset;
 * this avoids re-fetching it on every page visit. On failure the cache is
 * cleared so a subsequent call retries the fetch.
 */
var SpecialistService = (function () {
    var cache = null;

    function load() {
        if (!cache) {
            cache = fetch('services/specialists-sydney-hills-district.json')
                .then(function (res) { return res.json(); })
                .catch(function (err) {
                    cache = null;
                    throw err;
                });
        }
        return cache;
    }

    return { load: load };
})();

window.SpecialistService = SpecialistService;
