// Generated by @harness-engineering/linter-gen
// Do not edit manually - regenerate from harness-linter.yml
// Config: {{meta.configPath}}

import { ESLintUtils, type TSESTree } from '@typescript-eslint/utils';
import { minimatch } from 'minimatch';

const createRule = ESLintUtils.RuleCreator(
  () => 'https://github.com/harness-engineering/linter-gen'
);

type MessageIds = 'missingSchema';

const FILE_PATTERN = '{{{json config.pattern}}}';
const REQUIRE_ZOD_SCHEMA = {{config.requireZodSchema}};
const MESSAGE = '{{{config.message}}}';

/**
 * Normalize path separators to forward slashes
 */
function normalizePath(filePath: string): string {
  return filePath.replace(/\\/g, '/');
}

/**
 * Check if a path matches a glob pattern
 */
function matchesPattern(filePath: string, pattern: string): boolean {
  const normalized = normalizePath(filePath);
  return minimatch(normalized, pattern, { matchBase: true });
}

export default createRule<[], MessageIds>({
  name: '{{name}}',
  meta: {
    type: 'problem',
    docs: {
      description: MESSAGE,
    },
    messages: {
      missingSchema: MESSAGE,
    },
    schema: [],
  },
  defaultOptions: [],
  create(context) {
    const filePath = normalizePath(context.filename);

    // Only apply to files matching pattern
    if (!matchesPattern(filePath, FILE_PATTERN)) {
      return {};
    }

    if (!REQUIRE_ZOD_SCHEMA) {
      return {};
    }

    let hasZodSchema = false;
    let hasExport = false;

    return {
      // Check for Zod imports
      ImportDeclaration(node: TSESTree.ImportDeclaration) {
        if (node.source.value === 'zod') {
          hasZodSchema = true;
        }
      },

      // Check for z.object, z.string, etc.
      CallExpression(node: TSESTree.CallExpression) {
        if (
          node.callee.type === 'MemberExpression' &&
          node.callee.object.type === 'Identifier' &&
          node.callee.object.name === 'z'
        ) {
          hasZodSchema = true;
        }
      },

      // Track exports
      ExportNamedDeclaration() {
        hasExport = true;
      },
      ExportDefaultDeclaration() {
        hasExport = true;
      },

      // Check at end of file
      'Program:exit'(node: TSESTree.Program) {
        if (hasExport && !hasZodSchema) {
          context.report({
            node,
            messageId: 'missingSchema',
          });
        }
      },
    };
  },
});
