// ================================================================
// frontend/src/api/services/demo.ts
// ================================================================
/**
 * Demo Session Service - Complete backend alignment
 *
 * Backend API structure (3-tier architecture):
 * - ATOMIC: demo_accounts.py, demo_sessions.py
 * - OPERATIONS: demo_operations.py
 *
 * Note: Demo service does NOT use tenant prefix
 * 
 * Last Updated: 2025-10-05
 * Status: ✅ Complete - Zero drift with backend
 */

import { apiClient } from '../client';
import type { DemoSessionResponse } from '../types/demo';

export interface DemoAccount {
  account_type: string;
  email: string;
  name: string;
  password: string;
  description?: string;
  features?: string[];
  business_model?: string;
}

// Use the complete type from types/demo.ts which matches backend response
export type DemoSession = DemoSessionResponse;

export interface CreateSessionRequest {
  demo_account_type: 'individual_bakery' | 'central_baker';
}

export interface ExtendSessionRequest {
  session_id: string;
}

export interface DestroySessionRequest {
 session_id: string;
}

export interface ServiceProgress {
  status: 'not_started' | 'in_progress' | 'completed' | 'failed';
  records_cloned: number;
  error?: string;
}

export interface SessionStatusResponse {
  session_id: string;
  status: 'pending' | 'ready' | 'partial' | 'failed' | 'active' | 'expired' | 'destroyed';
  total_records_cloned: number;
  progress?: Record<string, ServiceProgress>;
  errors?: Array<{ service: string; error_message: string }>;
}

// ===================================================================
// OPERATIONS: Demo Session Status and Cloning
// ===================================================================

/**
 * Get session status
 * GET /demo/sessions/{session_id}/status
 */
export const getSessionStatus = async (sessionId: string): Promise<SessionStatusResponse> => {
  return await apiClient.get<SessionStatusResponse>(`/demo/sessions/${sessionId}/status`);
};

/**
 * Retry data cloning for a session
 * POST /demo/sessions/{session_id}/retry
 */
export const retryCloning = async (sessionId: string): Promise<SessionStatusResponse> => {
  return await apiClient.post<SessionStatusResponse>(`/demo/sessions/${sessionId}/retry`, {});
};

// ===================================================================
// ATOMIC: Demo Accounts
// Backend: services/demo_session/app/api/demo_accounts.py
// ===================================================================

/**
 * Get available demo accounts
 * GET /demo/accounts
 */
export const getDemoAccounts = async (): Promise<DemoAccount[]> => {
  return await apiClient.get<DemoAccount[]>('/demo/accounts');
};

// ===================================================================
// ATOMIC: Demo Sessions
// Backend: services/demo_session/app/api/demo_sessions.py
// ===================================================================

/**
 * Create a new demo session
 * POST /demo/sessions
 */
export const createDemoSession = async (
  request: CreateSessionRequest
): Promise<DemoSession> => {
  return await apiClient.post<DemoSession>('/demo/sessions', request);
};

/**
 * Get demo session details
 * GET /demo/sessions/{session_id}
 */
export const getDemoSession = async (sessionId: string): Promise<any> => {
  return await apiClient.get(`/demo/sessions/${sessionId}`);
};

// ===================================================================
// OPERATIONS: Demo Session Management
// Backend: services/demo_session/app/api/demo_operations.py
// ===================================================================

/**
 * Extend an existing demo session
 * POST /demo/sessions/{session_id}/extend
 */
export const extendDemoSession = async (
  request: ExtendSessionRequest
): Promise<DemoSession> => {
  return await apiClient.post<DemoSession>(
    `/demo/sessions/${request.session_id}/extend`,
    {}
  );
};

/**
 * Destroy a demo session
 * Note: This might be a DELETE endpoint - verify backend implementation
 */
export const destroyDemoSession = async (
  request: DestroySessionRequest
): Promise<{ message: string }> => {
  return await apiClient.post<{ message: string }>(
    `/demo/sessions/${request.session_id}/destroy`,
    {}
  );
};

/**
 * Get demo session statistics
 * GET /demo/stats
 */
export const getDemoStats = async (): Promise<any> => {
  return await apiClient.get('/demo/stats');
};

/**
 * Cleanup expired demo sessions (Admin/Operations)
 * POST /demo/operations/cleanup
 */
export const cleanupExpiredSessions = async (): Promise<any> => {
  return await apiClient.post('/demo/operations/cleanup', {});
};

// ===================================================================
// API Service Class
// ===================================================================

export class DemoSessionAPI {
  async getDemoAccounts(): Promise<DemoAccount[]> {
    return getDemoAccounts();
  }

  async createDemoSession(request: CreateSessionRequest): Promise<DemoSession> {
    return createDemoSession(request);
  }

  async getDemoSession(sessionId: string): Promise<any> {
    return getDemoSession(sessionId);
  }

  async extendDemoSession(request: ExtendSessionRequest): Promise<DemoSession> {
    return extendDemoSession(request);
  }

  async destroyDemoSession(request: DestroySessionRequest): Promise<{ message: string }> {
    return destroyDemoSession(request);
  }

  async getDemoStats(): Promise<any> {
    return getDemoStats();
  }

  async cleanupExpiredSessions(): Promise<any> {
    return cleanupExpiredSessions();
  }

  async getSessionStatus(sessionId: string): Promise<SessionStatusResponse> {
    return getSessionStatus(sessionId);
  }

  async retryCloning(sessionId: string): Promise<SessionStatusResponse> {
    return retryCloning(sessionId);
  }
}

export const demoSessionAPI = new DemoSessionAPI();
