import { http, HttpResponse } from 'msw';

const BASE_URL = 'http://localhost:9000/wp-json/sequensy/v1';

// Mock data
export const mockTriggers = [
  {
    type: 'wordpress/user_registration',
    label: 'User Registration',
    description: 'Triggered when a new user registers',
    integration: 'wordpress',
    available_fields: [
      { name: 'user_id', type: 'integer', description: 'User ID', example: '123' },
      {
        name: 'user_email',
        type: 'string',
        description: 'User email',
        example: 'user@example.com',
      },
      { name: 'user_login', type: 'string', description: 'Username', example: 'johndoe' },
    ],
    is_available: true,
    availability_reason: '',
  },
  {
    type: 'wordpress/post_published',
    label: 'Post Published',
    description: 'Triggered when a post is published',
    integration: 'wordpress',
    available_fields: [
      { name: 'post_id', type: 'integer', description: 'Post ID', example: '456' },
      { name: 'post_title', type: 'string', description: 'Post title', example: 'Hello World' },
    ],
    is_available: true,
    availability_reason: '',
  },
];

export const mockActions = [
  {
    type: 'wordpress/send_email',
    label: 'Send Email',
    description: 'Send an email notification',
    integration: 'wordpress',
    schema: {
      to: { type: 'string', label: 'To', description: 'Recipient email', required: true },
      subject: { type: 'string', label: 'Subject', description: 'Email subject', required: true },
      message: { type: 'text', label: 'Message', description: 'Email body', required: true },
    },
    output_fields: [],
    is_available: true,
    availability_reason: '',
  },
  {
    type: 'wordpress/create_post',
    label: 'Create Post',
    description: 'Create a new WordPress post',
    integration: 'wordpress',
    schema: {
      post_title: { type: 'string', label: 'Title', description: 'Post title', required: true },
      post_content: {
        type: 'text',
        label: 'Content',
        description: 'Post content',
        required: false,
      },
      post_status: {
        type: 'select',
        label: 'Status',
        description: 'Post status',
        required: false,
        options: [
          { value: 'draft', label: 'Draft' },
          { value: 'publish', label: 'Published' },
        ],
      },
    },
    output_fields: [{ name: 'post_id', type: 'integer', description: 'Created post ID' }],
    is_available: true,
    availability_reason: '',
  },
  {
    type: 'mailchimp/add_subscriber',
    label: 'Add Subscriber',
    description: 'Add a subscriber to a Mailchimp list',
    integration: 'mailchimp',
    schema: {
      list_id: {
        type: 'select',
        label: 'List',
        description: 'Mailchimp list',
        required: true,
        dynamic: true,
      },
      email: { type: 'string', label: 'Email', description: 'Subscriber email', required: true },
    },
    output_fields: [],
    is_available: true,
    availability_reason: '',
  },
];

export const mockWorkflows = [
  {
    id: 1,
    name: 'Welcome Email Workflow',
    status: 'active',
    trigger_type: 'wordpress/user_registration',
    config: {
      trigger: { type: 'wordpress/user_registration', config: {} },
      actions: [
        {
          label: 'send_email_1',
          type: 'wordpress/send_email',
          config: {
            to: '{{Trigger.user_email}}',
            subject: 'Welcome!',
            message: 'Thanks for registering!',
          },
        },
      ],
      layout: { nodes: [], edges: [] },
    },
  },
  {
    id: 2,
    name: 'Test Workflow',
    status: 'inactive',
    trigger_type: 'wordpress/post_published',
    config: {
      trigger: { type: 'wordpress/post_published', config: {} },
      actions: [],
      layout: { nodes: [], edges: [] },
    },
  },
];

export const mockIntegrations = [
  {
    id: 'mailchimp',
    name: 'Mailchimp',
    description: 'Email marketing platform',
    is_connected: false,
    requires_connection: true,
  },
  {
    id: 'wordpress',
    name: 'WordPress',
    description: 'Core WordPress functionality',
    is_connected: true,
    requires_connection: false,
  },
];

export const handlers = [
  // Triggers
  http.get(`${BASE_URL}/triggers`, () => {
    return HttpResponse.json(mockTriggers);
  }),

  // Actions
  http.get(`${BASE_URL}/actions`, () => {
    return HttpResponse.json(mockActions);
  }),

  // Workflows - List
  http.get(`${BASE_URL}/workflows`, ({ request }) => {
    const url = new URL(request.url);
    const status = url.searchParams.get('status');

    let workflows = [...mockWorkflows];
    if (status) {
      workflows = workflows.filter((w) => w.status === status);
    }

    return HttpResponse.json(workflows);
  }),

  // Workflows - Get single
  http.get(`${BASE_URL}/workflows/:id`, ({ params }) => {
    const workflow = mockWorkflows.find((w) => w.id === Number(params.id));
    if (!workflow) {
      return HttpResponse.json(
        { code: 'rest_workflow_invalid_id', message: 'Workflow not found' },
        { status: 404 },
      );
    }
    return HttpResponse.json(workflow);
  }),

  // Workflows - Create
  http.post(`${BASE_URL}/workflows`, async ({ request }) => {
    const data = (await request.json()) as Record<string, unknown>;
    return HttpResponse.json({ id: 999, status: 'inactive', ...data }, { status: 201 });
  }),

  // Workflows - Update
  http.put(`${BASE_URL}/workflows/:id`, async ({ request, params }) => {
    const data = (await request.json()) as Record<string, unknown>;
    return HttpResponse.json({ id: Number(params.id), ...data });
  }),

  // Workflows - Delete
  http.delete(`${BASE_URL}/workflows/:id`, ({ params }) => {
    const workflow = mockWorkflows.find((w) => w.id === Number(params.id));
    if (!workflow) {
      return HttpResponse.json(
        { code: 'rest_workflow_invalid_id', message: 'Workflow not found' },
        { status: 404 },
      );
    }
    return HttpResponse.json({ deleted: true });
  }),

  // Workflows - Activate
  http.post(`${BASE_URL}/workflows/:id/activate`, ({ params }) => {
    const workflow = mockWorkflows.find((w) => w.id === Number(params.id));
    if (!workflow) {
      return HttpResponse.json(
        { code: 'rest_workflow_invalid_id', message: 'Workflow not found' },
        { status: 404 },
      );
    }
    return HttpResponse.json({ ...workflow, status: 'active' });
  }),

  // Workflows - Deactivate
  http.post(`${BASE_URL}/workflows/:id/deactivate`, ({ params }) => {
    const workflow = mockWorkflows.find((w) => w.id === Number(params.id));
    if (!workflow) {
      return HttpResponse.json(
        { code: 'rest_workflow_invalid_id', message: 'Workflow not found' },
        { status: 404 },
      );
    }
    return HttpResponse.json({ ...workflow, status: 'inactive' });
  }),

  // Integrations
  http.get(`${BASE_URL}/integrations`, () => {
    return HttpResponse.json(mockIntegrations);
  }),

  // Settings
  http.get(`${BASE_URL}/settings`, () => {
    return HttpResponse.json({
      debug_mode: false,
      email_notifications: true,
      log_retention: 30,
      max_retries: 3,
      retry_delay: 1,
      webhook_timeout: 30,
    });
  }),

  http.post(`${BASE_URL}/settings`, async ({ request }) => {
    const data = await request.json();
    return HttpResponse.json(data);
  }),
];
