/**
 * Mock Project Data for Demo Mode
 *
 * Sample projects used by MSW handlers in demo mode.
 */

import type { ProjectResponse, ProjectStatus } from '@/lib/api/generated';

export interface ProjectListItem extends ProjectResponse {
  // Extended UI fields (computed/stored separately in real app)
  complexity?: 'script' | 'simple' | 'medium' | 'complex';
  progress?: number;
  openIssues?: number;
  activeAgents?: number;
  lastActivity?: string;
  tags?: string[];
  ownerName?: string;
}

export const sampleProjects: ProjectListItem[] = [
  {
    id: 'proj-001',
    name: 'E-Commerce Platform Redesign',
    slug: 'ecommerce-redesign',
    description: 'Complete redesign of the e-commerce platform with modern UI/UX',
    autonomy_level: 'milestone',
    status: 'active',
    settings: {},
    owner_id: 'user-001',
    created_at: '2025-11-15T10:00:00Z',
    updated_at: new Date(Date.now() - 2 * 60 * 1000).toISOString(),
    agent_count: 5,
    issue_count: 70,
    active_sprint_name: 'Sprint 3',
    // Extended fields
    complexity: 'complex',
    progress: 67,
    openIssues: 12,
    activeAgents: 4,
    lastActivity: '2 minutes ago',
    tags: ['e-commerce', 'frontend', 'ux'],
    ownerName: 'Felipe Cardoso',
  },
  {
    id: 'proj-002',
    name: 'Mobile Banking App',
    slug: 'mobile-banking',
    description: 'Native mobile app for banking services with biometric authentication',
    autonomy_level: 'full_control',
    status: 'active',
    settings: {},
    owner_id: 'user-001',
    created_at: '2025-11-20T09:00:00Z',
    updated_at: new Date(Date.now() - 15 * 60 * 1000).toISOString(),
    agent_count: 5,
    issue_count: 45,
    active_sprint_name: 'Sprint 2',
    complexity: 'complex',
    progress: 45,
    openIssues: 8,
    activeAgents: 5,
    lastActivity: '15 minutes ago',
    tags: ['mobile', 'fintech', 'security'],
    ownerName: 'Felipe Cardoso',
  },
  {
    id: 'proj-003',
    name: 'Internal HR Portal',
    slug: 'hr-portal',
    description: 'Employee self-service portal for HR operations',
    autonomy_level: 'milestone',
    status: 'paused',
    settings: {},
    owner_id: 'user-002',
    created_at: '2025-10-01T08:00:00Z',
    updated_at: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString(),
    agent_count: 3,
    issue_count: 25,
    active_sprint_name: 'Sprint 1',
    complexity: 'medium',
    progress: 23,
    openIssues: 5,
    activeAgents: 0,
    lastActivity: '2 days ago',
    tags: ['internal', 'hr', 'portal'],
    ownerName: 'Maria Santos',
  },
  {
    id: 'proj-004',
    name: 'API Gateway Modernization',
    slug: 'api-gateway',
    description: 'Migrate legacy API gateway to cloud-native architecture',
    autonomy_level: 'autonomous',
    status: 'active',
    settings: {},
    owner_id: 'user-001',
    created_at: '2025-12-01T11:00:00Z',
    updated_at: new Date(Date.now() - 60 * 60 * 1000).toISOString(),
    agent_count: 3,
    issue_count: 40,
    active_sprint_name: 'Sprint 4',
    complexity: 'complex',
    progress: 82,
    openIssues: 3,
    activeAgents: 2,
    lastActivity: '1 hour ago',
    tags: ['api', 'cloud', 'infrastructure'],
    ownerName: 'Felipe Cardoso',
  },
  {
    id: 'proj-005',
    name: 'Customer Analytics Dashboard',
    slug: 'analytics-dashboard',
    description: 'Real-time analytics dashboard for customer behavior insights',
    autonomy_level: 'milestone',
    status: 'completed',
    settings: {},
    owner_id: 'user-003',
    created_at: '2025-09-01T10:00:00Z',
    updated_at: new Date(Date.now() - 14 * 24 * 60 * 60 * 1000).toISOString(),
    agent_count: 0,
    issue_count: 50,
    active_sprint_name: null,
    complexity: 'medium',
    progress: 100,
    openIssues: 0,
    activeAgents: 0,
    lastActivity: '2 weeks ago',
    tags: ['analytics', 'ml', 'dashboard'],
    ownerName: 'Alex Johnson',
  },
  {
    id: 'proj-006',
    name: 'DevOps Pipeline Automation',
    slug: 'devops-automation',
    description: 'Automate CI/CD pipelines with AI-assisted deployments',
    autonomy_level: 'milestone',
    status: 'active',
    settings: {},
    owner_id: 'user-001',
    created_at: '2025-12-10T14:00:00Z',
    updated_at: new Date(Date.now() - 30 * 60 * 1000).toISOString(),
    agent_count: 4,
    issue_count: 30,
    active_sprint_name: 'Sprint 1',
    complexity: 'medium',
    progress: 35,
    openIssues: 6,
    activeAgents: 3,
    lastActivity: '30 minutes ago',
    tags: ['devops', 'automation', 'ci-cd'],
    ownerName: 'Felipe Cardoso',
  },
  {
    id: 'proj-007',
    name: 'Inventory Management System',
    slug: 'inventory-system',
    description: 'Warehouse inventory tracking with barcode scanning',
    autonomy_level: 'full_control',
    status: 'archived',
    settings: {},
    owner_id: 'user-002',
    created_at: '2025-06-15T08:00:00Z',
    updated_at: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString(),
    agent_count: 0,
    issue_count: 80,
    active_sprint_name: null,
    complexity: 'simple',
    progress: 100,
    openIssues: 0,
    activeAgents: 0,
    lastActivity: '1 month ago',
    tags: ['inventory', 'warehouse', 'logistics'],
    ownerName: 'Maria Santos',
  },
  {
    id: 'proj-008',
    name: 'Customer Support Chatbot',
    slug: 'support-chatbot',
    description: 'AI-powered chatbot for 24/7 customer support',
    autonomy_level: 'autonomous',
    status: 'active',
    settings: {},
    owner_id: 'user-003',
    created_at: '2025-12-05T09:00:00Z',
    updated_at: new Date(Date.now() - 45 * 60 * 1000).toISOString(),
    agent_count: 3,
    issue_count: 35,
    active_sprint_name: 'Sprint 2',
    complexity: 'medium',
    progress: 58,
    openIssues: 4,
    activeAgents: 2,
    lastActivity: '45 minutes ago',
    tags: ['ai', 'chatbot', 'support'],
    ownerName: 'Alex Johnson',
  },
];

// In-memory store for demo mode (allows create/update/delete)
let projectsStore = [...sampleProjects];

export function getProjects(): ProjectListItem[] {
  return projectsStore;
}

export function getProjectById(id: string): ProjectListItem | undefined {
  return projectsStore.find((p) => p.id === id);
}

export function getProjectBySlug(slug: string): ProjectListItem | undefined {
  return projectsStore.find((p) => p.slug === slug);
}

export function createProject(data: Partial<ProjectListItem>): ProjectListItem {
  const newProject: ProjectListItem = {
    id: `proj-${Date.now()}`,
    name: data.name || 'New Project',
    slug: data.slug || data.name?.toLowerCase().replace(/\s+/g, '-') || `project-${Date.now()}`,
    description: data.description || null,
    autonomy_level: data.autonomy_level || 'milestone',
    status: 'active',
    settings: data.settings || {},
    owner_id: data.owner_id || 'user-001',
    created_at: new Date().toISOString(),
    updated_at: new Date().toISOString(),
    agent_count: 0,
    issue_count: 0,
    active_sprint_name: null,
    complexity: data.complexity || 'medium',
    progress: 0,
    openIssues: 0,
    activeAgents: 0,
    lastActivity: 'Just now',
    tags: data.tags || [],
    ownerName: 'Demo User',
  };
  projectsStore.unshift(newProject);
  return newProject;
}

export function updateProject(
  id: string,
  data: Partial<ProjectListItem>
): ProjectListItem | undefined {
  const index = projectsStore.findIndex((p) => p.id === id);
  if (index === -1) return undefined;

  projectsStore[index] = {
    ...projectsStore[index],
    ...data,
    updated_at: new Date().toISOString(),
  };
  return projectsStore[index];
}

export function deleteProject(id: string): boolean {
  const index = projectsStore.findIndex((p) => p.id === id);
  if (index === -1) return false;
  projectsStore.splice(index, 1);
  return true;
}

export function resetProjects(): void {
  projectsStore = [...sampleProjects];
}
