import client, { ensureAuthenticated } from '../lib/directus';
import { createCollection, createField } from '@directus/sdk';

async function setupSchema() {
  console.log('🏗️  Manually creating contact_submissions collection...');

  try {
    // 1. Authenticate (using token from config)
    await ensureAuthenticated();

    // 2. Create collection
    console.log('📡 Creating "contact_submissions" collection...');
    await client.request(
      createCollection({
        collection: 'contact_submissions',
        meta: {
          icon: 'contact_mail',
          color: '#002b49',
          display_template: '{{name}} | {{email}}',
        },
        schema: {
          name: 'contact_submissions',
        },
      }),
    );

    // 3. Create fields
    console.log('📡 Creating fields for "contact_submissions"...');

    // name
    await client.request(
      createField('contact_submissions', {
        field: 'name',
        type: 'string',
        meta: { interface: 'input' },
      }),
    );

    // email
    await client.request(
      createField('contact_submissions', {
        field: 'email',
        type: 'string',
        meta: { interface: 'input' },
      }),
    );

    // message
    await client.request(
      createField('contact_submissions', {
        field: 'message',
        type: 'text',
        meta: { interface: 'textarea' },
      }),
    );

    console.log('✨ Collection and fields created successfully!');
  } catch (error: any) {
    console.error('❌ Error creating schema:');
    if (error.errors) {
      console.error(JSON.stringify(error.errors, null, 2));
    } else {
      console.error(error.message || error);
    }
  }
}

setupSchema();
