import type {Store} from '@/features/stores/types/store';
import type {RangeKey} from '@/shared/types/api';
import {buildTimeseries} from '@/features/dashboard/mock/dashboard.mock';

/**
 * The store roster. Names, staffing and status are fixed so the switcher, the
 * comparison panel and the Store module all agree; the numbers derive from the
 * shared timeseries so a store's card and its dashboard cannot disagree.
 */
export const STORE_SEED: {
  id: string;
  name: string;
  city: string;
  staffCount: number;
  status: Store['status'];
  /**
   * Traffic multiplier relative to a typical store. This is the ONLY place a
   * store's size is defined — every generator multiplies by it, so a store's
   * dashboard, its comparison card and its row in the Store module cannot
   * disagree about how big it is.
   *
   * The spread is deliberate: the flagship genuinely outperforms and
   * Whitefield genuinely struggles (it's also the one Loyaly AI flags).
   * Fixtures that are uniformly average make a comparison view look like it
   * works while showing nothing.
   */
  scale: number;
}[] = [
  {
    id: 'blr-indiranagar',
    name: 'Indiranagar Flagship',
    city: 'Bengaluru',
    staffCount: 14,
    status: 'open',
    scale: 1.6,
  },
  {
    id: 'blr-koramangala',
    name: 'Koramangala',
    city: 'Bengaluru',
    staffCount: 9,
    status: 'open',
    scale: 1.0,
  },
  {
    id: 'blr-whitefield',
    name: 'Whitefield',
    city: 'Bengaluru',
    staffCount: 7,
    status: 'maintenance',
    scale: 0.62,
  },
  {
    id: 'che-anna-nagar',
    name: 'Anna Nagar',
    city: 'Chennai',
    staffCount: 8,
    status: 'open',
    scale: 0.85,
  },
  {
    id: 'hyd-jubilee',
    name: 'Jubilee Hills',
    city: 'Hyderabad',
    staffCount: 11,
    status: 'closed',
    scale: 1.15,
  },
];

export function storeName(id: string): string {
  return STORE_SEED.find((s) => s.id === id)?.name ?? 'All stores';
}

export function storeExists(id: string): boolean {
  return STORE_SEED.some((s) => s.id === id);
}

/** Traffic multiplier for a store id; 'all' is the sum of every store. */
export function storeScale(id: string): number {
  if (id === 'all') {
    return STORE_SEED.reduce((a, s) => a + s.scale, 0);
  }
  return STORE_SEED.find((s) => s.id === id)?.scale ?? 1;
}

/**
 * Totals per store, derived from the SAME buildTimeseries() the dashboard
 * charts use. Generating these independently is how a store's card ends up
 * claiming a different revenue than the store's own dashboard.
 */
export function buildStores(range: RangeKey, endMs: number): Store[] {
  return STORE_SEED.map((s) => {
    const points = buildTimeseries(range, s.id, endMs);
    const visitors = points.reduce((a, p) => a + p.visitors, 0);
    const purchases = points.reduce((a, p) => a + p.purchases, 0);

    return {
      id: s.id,
      name: s.name,
      status: s.status,
      visitors,
      purchases,
      revenueInr: points.reduce((a, p) => a + p.revenue, 0),
      conversionPct: visitors
        ? Number(((purchases / visitors) * 100).toFixed(1))
        : 0,
      staffCount: s.staffCount,
    };
  });
}

export function buildStore(
  id: string,
  range: RangeKey,
  endMs: number,
): Store | null {
  return buildStores(range, endMs).find((s) => s.id === id) ?? null;
}
