'use client';
import { create } from 'zustand';
import { formatLocalDate } from '@/lib/format';
import { mergeWorkActivities, type WorkActivity, type WorkActivityDraft } from '@/lib/work-activity';
import { makeProgressNoteActivity, type ProgressNoteActivityInput } from '@/lib/work-activity-factory';
import { loadServerData, saveServerData, SERVER_DATA_CACHE_MS } from '@/lib/server-data';

interface WorkActivityState {
  activities: WorkActivity[];
  loaded: boolean;
  fetchActivities: (options?: { force?: boolean }) => Promise<void>;
  addActivity: (data: WorkActivityDraft) => WorkActivity;
  addProgressNote: (data: ProgressNoteActivityInput) => WorkActivity;
  deleteActivity: (id: string) => void;
}

let lastActivitiesFetchAt = 0;

async function saveStored(
  items: WorkActivity[],
  setActivities?: (items: WorkActivity[]) => void,
  options: { mergeRemote?: boolean } = {},
) {
  try {
    if (options.mergeRemote === false) {
      await saveServerData('work-activities', items);
      return;
    }
    const remote = await loadServerData<WorkActivity[]>('work-activities', { force: true });
    const merged = mergeWorkActivities(Array.isArray(remote) ? remote : [], items);
    setActivities?.(merged);
    await saveServerData('work-activities', merged);
  } catch {
    saveServerData('work-activities', items).catch(() => {});
  }
}

async function loadStored(): Promise<WorkActivity[] | null> {
  try {
    return await loadServerData<WorkActivity[]>('work-activities');
  } catch {}
  return null;
}

function createActivityId(): string {
  return `act-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`;
}

export const useWorkActivityStore = create<WorkActivityState>((set, get) => ({
  activities: [],
  loaded: false,

  fetchActivities: async (options) => {
    if (!options?.force && get().loaded && Date.now() - lastActivitiesFetchAt < SERVER_DATA_CACHE_MS) return;
    const cached = await loadStored();
    if (!options?.force && get().loaded && Date.now() - lastActivitiesFetchAt < SERVER_DATA_CACHE_MS) return;
    lastActivitiesFetchAt = Date.now();
    set({ activities: cached ?? [], loaded: true });
  },

  addActivity: (data) => {
    const now = new Date();
    const item: WorkActivity = {
      ...data,
      id: createActivityId(),
      date: data.date ?? formatLocalDate(now),
      occurredAt: data.occurredAt ?? now.toISOString(),
    };
    const updated = [...get().activities, item];
    set({ activities: updated, loaded: true });
    saveStored(updated, (activities) => set({ activities, loaded: true }));
    return item;
  },

  addProgressNote: (data) => {
    return get().addActivity(makeProgressNoteActivity(data));
  },

  deleteActivity: (id) => {
    const updated = get().activities.filter((activity) => activity.id !== id);
    set({ activities: updated, loaded: true });
    saveStored(updated, (activities) => set({ activities, loaded: true }), { mergeRemote: false });
  },
}));
