import { create } from 'zustand';
import { apiFetch } from '../lib/api';

export type ProtectedDeviceStatus = 'pending' | 'active' | 'revoked' | 'degraded';

export interface ProtectedDevice {
  id: string;
  platform: 'mac' | 'windows' | string;
  label: string;
  status: ProtectedDeviceStatus;
  installedAt: string | null;
  createdAt: string;
}

export interface EnrollResult {
  deviceId: string;
  downloadUrl: string;
}

type ProtectedDevicesState = {
  devices: ProtectedDevice[];
  loading: boolean;
  enrolling: boolean;

  load: () => Promise<void>;
  enroll: (label: string, platform: 'mac' | 'windows') => Promise<EnrollResult>;
  confirmInstalled: (id: string) => Promise<void>;
  remove: (id: string) => Promise<{ manualRemovalRequired: boolean }>;
  reset: () => void;
};

export const useProtectedDevicesStore = create<ProtectedDevicesState>((set, get) => ({
  devices: [],
  loading: false,
  enrolling: false,

  load: async () => {
    set({ loading: true });
    try {
      const res = await apiFetch<{ devices?: ProtectedDevice[] }>('/api/devices/protected');
      set({ devices: Array.isArray(res?.devices) ? res.devices : [] });
    } catch {
      // endpoint might not be ready yet — keep empty state, screen handles it
    } finally {
      set({ loading: false });
    }
  },

  enroll: async (label: string, platform: 'mac' | 'windows') => {
    set({ enrolling: true });
    try {
      const result = await apiFetch<EnrollResult>('/api/devices/enroll', {
        method: 'POST',
        body: { platform, label },
      });
      await get().load();
      return result;
    } finally {
      set({ enrolling: false });
    }
  },

  confirmInstalled: async (id: string) => {
    await apiFetch(`/api/devices/${id}/confirm-installed`, { method: 'POST' });
    set((s) => ({
      devices: s.devices.map((d) =>
        d.id === id ? { ...d, status: 'active' as const, installedAt: new Date().toISOString() } : d
      ),
    }));
  },

  remove: async (id: string) => {
    const res = await apiFetch<{ manualRemovalRequired: boolean }>(`/api/devices/${id}/revoke`, {
      method: 'DELETE',
    });
    set((s) => ({ devices: s.devices.filter((d) => d.id !== id) }));
    return res;
  },

  reset: () => set({ devices: [], loading: false, enrolling: false }),
}));
