import path from 'path';

/**
 * Rule: naming-convention/filename-matches-exported-definition
 * Ensures that the filename matches the exported definition
 */
export default {
  meta: {
    type: 'suggestion',
    docs: {
      description: 'Ensures that the filename matches the exported interface or type definition',
    },
  },
  create(context) {
    const filename = context.getFilename();
    const basename = path.basename(filename, '.ts'); // e.g., "IButtonProps" from "IButtonProps.ts"
    const expectedName = basename; // e.g., "IButtonProps"
    let exportVariableName = null;
    let currentNode = null;
    return {
      ExportDefaultDeclaration(node) {
        exportVariableName = node?.declaration?.name || node?.declaration?.id?.name;
        if (exportVariableName) {
          currentNode = node;
        }
      },
      'Program:exit'(node) {
        // Early return if validation passes
        if (exportVariableName === expectedName) {
          return;
        }
        context.report({
          node,
          message: `The file "${basename}.ts" must define an 'export default interface ${expectedName}' unless a type "${expectedName}" is defined.`,
        });
      },
    };
  },
};
