/**
 * seed.js
 * Database seeding script for development
 */

import sequelize from '../config/database.js';
import User from '../models/User.js';
import Project from '../models/Project.js';
import Token from '../models/Token.js';
import Component from '../models/Component.js';
import Icon from '../models/Icon.js';
import Integration from '../models/Integration.js';

const seedDatabase = async () => {
  try {
    console.log('[Seed] Synchronizing database...');
    await sequelize.sync({ force: process.env.FORCE_SEED === 'true' });

    // Create demo user (for auto-login in production)
    console.log('[Seed] Creating demo user...');
    const demoUser = await User.create({
      email: 'demo@example.com',
      password: 'demo123',
      name: 'Demo User',
      role: 'admin'
    });

    // Create test user
    console.log('[Seed] Creating test user...');
    const user = await User.create({
      email: 'test@example.com',
      password: 'password123',
      name: 'Test User',
      role: 'admin'
    });

    // Create test project
    console.log('[Seed] Creating test project...');
    const project = await Project.create({
      name: 'Test Design System',
      key: 'TDS',
      description: 'A test design system project',
      userId: user.id,
      figmaFileKey: 'ABC123DEF456',
      storybookUrl: 'https://storybook.example.com'
    });

    // Create test tokens
    console.log('[Seed] Creating test tokens...');
    const colorTokens = [
      { projectId: project.id, name: 'primary', category: 'color', value: '#0066CC', description: 'Primary brand color' },
      { projectId: project.id, name: 'secondary', category: 'color', value: '#4CAF50', description: 'Secondary brand color' },
      { projectId: project.id, name: 'error', category: 'color', value: '#F44336', description: 'Error color' }
    ];

    await Token.bulkCreate(colorTokens);

    const spacingTokens = [
      { projectId: project.id, name: 'xs', category: 'spacing', value: '4px', description: 'Extra small spacing' },
      { projectId: project.id, name: 'sm', category: 'spacing', value: '8px', description: 'Small spacing' },
      { projectId: project.id, name: 'md', category: 'spacing', value: '16px', description: 'Medium spacing' },
      { projectId: project.id, name: 'lg', category: 'spacing', value: '24px', description: 'Large spacing' }
    ];

    await Token.bulkCreate(spacingTokens);

    // Create test components
    console.log('[Seed] Creating test components...');
    const components = [
      {
        projectId: project.id,
        name: 'Button',
        category: 'input',
        description: 'Reusable button component',
        status: 'ready',
        adoptionScore: 0.95,
        usageCount: 150,
        variants: ['primary', 'secondary', 'danger', 'ghost']
      },
      {
        projectId: project.id,
        name: 'Card',
        category: 'layout',
        description: 'Card container component',
        status: 'ready',
        adoptionScore: 0.85,
        usageCount: 120,
        variants: ['elevated', 'outlined', 'filled']
      },
      {
        projectId: project.id,
        name: 'Modal',
        category: 'layout',
        description: 'Modal dialog component',
        status: 'wip',
        adoptionScore: 0.60,
        usageCount: 45,
        variants: ['default', 'alert']
      }
    ];

    await Component.bulkCreate(components);

    // Create test icons
    console.log('[Seed] Creating test icons...');
    const icons = [
      {
        projectId: project.id,
        name: 'home',
        category: 'navigation',
        svgData: '<svg viewBox="0 0 24 24"><path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>',
        tags: ['navigation', 'common']
      },
      {
        projectId: project.id,
        name: 'search',
        category: 'action',
        svgData: '<svg viewBox="0 0 24 24"><path d="M15.5 14h-.79l-.28-.27A6.471 6.471 0 0016 9.5 6.5 6.5 0 109.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/></svg>',
        tags: ['action', 'search']
      }
    ];

    await Icon.bulkCreate(icons);

    // Create test integrations
    console.log('[Seed] Creating test integrations...');
    const integrations = [
      {
        projectId: project.id,
        type: 'figma',
        name: 'Figma Design Tokens',
        config: { fileKey: 'ABC123DEF456', autoSync: true },
        isActive: true
      },
      {
        projectId: project.id,
        type: 'storybook',
        name: 'Storybook Documentation',
        config: { url: 'https://storybook.example.com', autoPublish: false },
        isActive: true
      }
    ];

    await Integration.bulkCreate(integrations);

    console.log('[Seed] Database seeding completed successfully!');
    console.log('[Seed] Demo user: demo@example.com / demo123 (for auto-login)');
    console.log('[Seed] Test user: test@example.com / password123');
    console.log('[Seed] Test project: Test Design System (key: TDS)');

    process.exit(0);
  } catch (error) {
    console.error('[Seed] Error seeding database:', error);
    process.exit(1);
  }
};

seedDatabase();
