import { drizzle } from 'drizzle-orm/neon-http';
import { neon } from '@neondatabase/serverless';
import { marketplaceListings } from './schema';

// ── MCP Server Templates ───────────────────────────────────────────────────────

interface TemplateEntry {
  name: string;
  slug: string;
  category: string;
  description: string;
  tags: string[];
  toolCount: number;
}

const templates: TemplateEntry[] = [
  // ─── CRM ──────────────────────────────────────────────────────────────────
  {
    name: 'Close CRM',
    slug: 'close',
    category: 'CRM',
    description: 'Full Close CRM integration — leads, contacts, opportunities, activities, and pipeline management.',
    tags: ['crm', 'sales', 'leads', 'pipeline'],
    toolCount: 42,
  },
  {
    name: 'Pipedrive',
    slug: 'pipedrive',
    category: 'CRM',
    description: 'Pipedrive CRM tools for deals, persons, organizations, activities, and pipeline stages.',
    tags: ['crm', 'sales', 'deals', 'pipeline'],
    toolCount: 38,
  },
  {
    name: 'Keap',
    slug: 'keap',
    category: 'CRM',
    description: 'Keap (Infusionsoft) CRM with contacts, deals, appointments, tasks, and email automation.',
    tags: ['crm', 'automation', 'contacts', 'email'],
    toolCount: 35,
  },
  {
    name: 'Housecall Pro',
    slug: 'housecall-pro',
    category: 'CRM',
    description: 'Housecall Pro for home service businesses — jobs, estimates, invoices, scheduling, and customers.',
    tags: ['crm', 'field-service', 'scheduling', 'invoicing'],
    toolCount: 30,
  },

  // ─── eCommerce ─────────────────────────────────────────────────────────────
  {
    name: 'BigCommerce',
    slug: 'bigcommerce',
    category: 'eCommerce',
    description: 'BigCommerce store management — products, orders, customers, categories, and inventory.',
    tags: ['ecommerce', 'products', 'orders', 'inventory'],
    toolCount: 45,
  },
  {
    name: 'Squarespace',
    slug: 'squarespace',
    category: 'eCommerce',
    description: 'Squarespace commerce and content tools — products, orders, pages, forms, and inventory.',
    tags: ['ecommerce', 'cms', 'website', 'products'],
    toolCount: 28,
  },
  {
    name: 'Lightspeed',
    slug: 'lightspeed',
    category: 'eCommerce',
    description: 'Lightspeed POS and eCommerce — products, sales, inventory, customers, and registers.',
    tags: ['ecommerce', 'pos', 'retail', 'inventory'],
    toolCount: 40,
  },
  {
    name: 'Clover',
    slug: 'clover',
    category: 'eCommerce',
    description: 'Clover POS integration — orders, inventory, merchants, employees, and payments.',
    tags: ['pos', 'payments', 'retail', 'inventory'],
    toolCount: 32,
  },

  // ─── HR ────────────────────────────────────────────────────────────────────
  {
    name: 'BambooHR',
    slug: 'bamboohr',
    category: 'HR',
    description: 'BambooHR people management — employees, time-off, benefits, reports, and onboarding.',
    tags: ['hr', 'employees', 'time-off', 'onboarding'],
    toolCount: 36,
  },
  {
    name: 'Gusto',
    slug: 'gusto',
    category: 'HR',
    description: 'Gusto payroll and HR — employees, payroll, benefits, time-tracking, and compliance.',
    tags: ['hr', 'payroll', 'benefits', 'compliance'],
    toolCount: 33,
  },
  {
    name: 'Rippling',
    slug: 'rippling',
    category: 'HR',
    description: 'Rippling workforce platform — employees, payroll, devices, apps, and policies.',
    tags: ['hr', 'payroll', 'it', 'workforce'],
    toolCount: 38,
  },

  // ─── Finance ───────────────────────────────────────────────────────────────
  {
    name: 'FreshBooks',
    slug: 'freshbooks',
    category: 'Finance',
    description: 'FreshBooks accounting — invoices, expenses, clients, time entries, and reports.',
    tags: ['finance', 'invoicing', 'accounting', 'expenses'],
    toolCount: 34,
  },
  {
    name: 'Wave',
    slug: 'wave',
    category: 'Finance',
    description: 'Wave accounting and invoicing — customers, invoices, transactions, and financial reports.',
    tags: ['finance', 'accounting', 'invoicing', 'free'],
    toolCount: 25,
  },
  {
    name: 'Toast',
    slug: 'toast',
    category: 'Finance',
    description: 'Toast restaurant POS — orders, menus, employees, reporting, and payment processing.',
    tags: ['finance', 'pos', 'restaurant', 'orders'],
    toolCount: 35,
  },

  // ─── Marketing ─────────────────────────────────────────────────────────────
  {
    name: 'Mailchimp',
    slug: 'mailchimp',
    category: 'Marketing',
    description: 'Mailchimp email marketing — campaigns, audiences, templates, automations, and analytics.',
    tags: ['marketing', 'email', 'campaigns', 'automation'],
    toolCount: 40,
  },
  {
    name: 'Brevo',
    slug: 'brevo',
    category: 'Marketing',
    description: 'Brevo (SendinBlue) marketing — email campaigns, SMS, contacts, automations, and transactional.',
    tags: ['marketing', 'email', 'sms', 'automation'],
    toolCount: 36,
  },
  {
    name: 'Constant Contact',
    slug: 'constant-contact',
    category: 'Marketing',
    description: 'Constant Contact email marketing — campaigns, contacts, lists, events, and reporting.',
    tags: ['marketing', 'email', 'contacts', 'events'],
    toolCount: 30,
  },
  {
    name: 'Meta Ads',
    slug: 'meta-ads',
    category: 'Marketing',
    description: 'Meta (Facebook/Instagram) advertising — campaigns, ad sets, ads, audiences, and insights.',
    tags: ['marketing', 'ads', 'facebook', 'instagram'],
    toolCount: 38,
  },

  // ─── Support ───────────────────────────────────────────────────────────────
  {
    name: 'Zendesk',
    slug: 'zendesk',
    category: 'Support',
    description: 'Zendesk customer support — tickets, users, organizations, macros, views, and satisfaction.',
    tags: ['support', 'tickets', 'helpdesk', 'customer-service'],
    toolCount: 50,
  },
  {
    name: 'Freshdesk',
    slug: 'freshdesk',
    category: 'Support',
    description: 'Freshdesk helpdesk — tickets, contacts, agents, groups, canned responses, and SLAs.',
    tags: ['support', 'tickets', 'helpdesk', 'sla'],
    toolCount: 42,
  },
  {
    name: 'Help Scout',
    slug: 'helpscout',
    category: 'Support',
    description: 'Help Scout customer communication — conversations, customers, mailboxes, tags, and workflows.',
    tags: ['support', 'conversations', 'email', 'customer-service'],
    toolCount: 32,
  },

  // ─── Project Management ───────────────────────────────────────────────────
  {
    name: 'Trello',
    slug: 'trello',
    category: 'ProjectMgmt',
    description: 'Trello boards, lists, and cards — full project management with labels, checklists, and members.',
    tags: ['project-management', 'kanban', 'boards', 'tasks'],
    toolCount: 35,
  },
  {
    name: 'ClickUp',
    slug: 'clickup',
    category: 'ProjectMgmt',
    description: 'ClickUp workspaces — tasks, spaces, folders, goals, time tracking, and custom fields.',
    tags: ['project-management', 'tasks', 'goals', 'time-tracking'],
    toolCount: 48,
  },
  {
    name: 'Wrike',
    slug: 'wrike',
    category: 'ProjectMgmt',
    description: 'Wrike project management — tasks, folders, projects, timesheets, comments, and workflows.',
    tags: ['project-management', 'tasks', 'workflows', 'collaboration'],
    toolCount: 38,
  },
  {
    name: 'Basecamp',
    slug: 'basecamp',
    category: 'ProjectMgmt',
    description: 'Basecamp project organization — to-dos, message boards, schedules, docs, and campfires.',
    tags: ['project-management', 'collaboration', 'to-dos', 'messaging'],
    toolCount: 28,
  },

  // ─── Scheduling ────────────────────────────────────────────────────────────
  {
    name: 'Acuity Scheduling',
    slug: 'acuity-scheduling',
    category: 'Scheduling',
    description: 'Acuity Scheduling — appointments, availability, calendars, clients, and intake forms.',
    tags: ['scheduling', 'appointments', 'calendar', 'booking'],
    toolCount: 22,
  },
  {
    name: 'Calendly',
    slug: 'calendly',
    category: 'Scheduling',
    description: 'Calendly scheduling automation — event types, invitees, availability, and webhook management.',
    tags: ['scheduling', 'calendar', 'booking', 'automation'],
    toolCount: 20,
  },

  // ─── Communication ────────────────────────────────────────────────────────
  {
    name: 'Twilio',
    slug: 'twilio',
    category: 'Communication',
    description: 'Twilio communications — SMS, voice calls, phone numbers, messaging services, and call logs.',
    tags: ['communication', 'sms', 'voice', 'messaging'],
    toolCount: 35,
  },

  // ─── Field Service ────────────────────────────────────────────────────────
  {
    name: 'ServiceTitan',
    slug: 'servicetitan',
    category: 'FieldService',
    description: 'ServiceTitan field service management — jobs, customers, technicians, invoices, and dispatch.',
    tags: ['field-service', 'jobs', 'dispatch', 'technicians'],
    toolCount: 45,
  },
  {
    name: 'Jobber',
    slug: 'jobber',
    category: 'FieldService',
    description: 'Jobber for service businesses — quotes, jobs, invoices, clients, scheduling, and routes.',
    tags: ['field-service', 'scheduling', 'invoicing', 'quotes'],
    toolCount: 32,
  },
  {
    name: 'FieldEdge',
    slug: 'fieldedge',
    category: 'FieldService',
    description: 'FieldEdge field service — dispatching, work orders, customers, invoicing, and equipment.',
    tags: ['field-service', 'dispatch', 'work-orders', 'invoicing'],
    toolCount: 30,
  },
  {
    name: 'TouchBistro',
    slug: 'touchbistro',
    category: 'FieldService',
    description: 'TouchBistro restaurant management — menus, orders, reservations, staff, and reporting.',
    tags: ['restaurant', 'pos', 'menus', 'reservations'],
    toolCount: 28,
  },

  // ─── Real Estate ──────────────────────────────────────────────────────────
  {
    name: 'Reonomy',
    slug: 'reonomy',
    category: 'RealEstate',
    description: 'Reonomy commercial real estate data — properties, ownership, sales history, and market analysis.',
    tags: ['real-estate', 'commercial', 'properties', 'data'],
    toolCount: 20,
  },

  // ─── DevTools ─────────────────────────────────────────────────────────────
  {
    name: 'Google Search Console',
    slug: 'google-console',
    category: 'DevTools',
    description: 'Google Search Console — search performance, URL inspection, sitemaps, and indexing.',
    tags: ['seo', 'google', 'analytics', 'search'],
    toolCount: 18,
  },

  // ─── Automation ───────────────────────────────────────────────────────────
  {
    name: 'n8n Apps',
    slug: 'n8n-apps',
    category: 'Automation',
    description: 'n8n workflow automation — workflows, executions, credentials, and webhook triggers.',
    tags: ['automation', 'workflows', 'integrations', 'webhooks'],
    toolCount: 25,
  },
  {
    name: 'CloseBot',
    slug: 'closebot',
    category: 'Automation',
    description: 'CloseBot AI sales automation — chatbots, conversations, leads, and appointment setting.',
    tags: ['automation', 'chatbot', 'sales', 'ai'],
    toolCount: 22,
  },
];

// ── Seed runner ────────────────────────────────────────────────────────────────

async function seed() {
  if (!process.env.DATABASE_URL) {
    throw new Error('DATABASE_URL environment variable is required');
  }

  const sql = neon(process.env.DATABASE_URL);
  const db = drizzle(sql);

  console.log('🌱 Seeding marketplace with 37 MCP server templates…');

  const now = new Date();

  const rows = templates.map((t) => ({
    name: t.name,
    slug: t.slug,
    description: t.description,
    category: t.category,
    tags: t.tags,
    toolCount: t.toolCount,
    appCount: 0,
    forkCount: 0,
    isOfficial: true,
    isFeatured: false,
    priceCents: 0,
    status: 'published' as const,
    createdAt: now,
    publishedAt: now,
  }));

  await db
    .insert(marketplaceListings)
    .values(rows)
    .onConflictDoNothing({ target: marketplaceListings.slug });

  console.log(`✅ Seeded ${rows.length} marketplace templates`);
  process.exit(0);
}

seed().catch((err) => {
  console.error('❌ Seed failed:', err);
  process.exit(1);
});
