// Copyright (c) 2026 Sidero Labs, Inc.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
import type { Meta, StoryObj } from '@storybook/vue3-vite'
import { http, HttpResponse } from 'msw'
import { fn } from 'storybook/test'

import type { Empty } from '@/api/google/protobuf/empty.pb'
import { Code } from '@/api/google/rpc/code.pb'
import type { ValidateConfigRequest } from '@/api/omni/management/management.pb'

import ConfigPatchEdit from './ConfigPatchEdit.vue'

const meta: Meta<typeof ConfigPatchEdit> = {
  component: ConfigPatchEdit,
  decorators: [() => ({ template: '<div class="fixed inset-0"><story/></div>' })],
  args: {
    id: 'Machine Set main worker pool',
    config: `# Machine config patch for node "foo-bar"

# You can write partial Talos machine config here which will override the default
# Talos machine config for this machine generated by Omni.

# example (changing the node hostname):
apiVersion: v1alpha1
kind: HostnameConfig
hostname: "foo-bar"
auto: off`,
    onSave: fn(),
  },
}

export default meta
type Story = StoryObj<typeof meta>

export const Default: Story = {
  parameters: {
    msw: {
      handlers: [
        http.post<never, ValidateConfigRequest, Empty>(
          '/management.ManagementService/ValidateConfig',
          () => HttpResponse.json({}),
        ),
      ],
    },
  },
}

export const InvalidConfigError: Story = {
  args: {
    config: 'Try saving me',
  },
  parameters: {
    msw: {
      handlers: [
        http.post<never, ValidateConfigRequest, Empty>(
          '/management.ManagementService/ValidateConfig',
          () =>
            HttpResponse.json(
              { code: Code.INVALID_ARGUMENT, message: 'Everything is bad!' },
              { status: 400 },
            ),
        ),
      ],
    },
  },
}
