/**
 * ESLint rules for Filename Rules
 * 
 * Enforces correct file naming conventions
 */

module.exports = {
  // Rule 1: Presenter filename must match class name
  'presenter-filename-must-match-class': {
    meta: {
      type: 'problem',
      docs: {
        description: 'Enforce presenter filename matches class name',
        category: 'Filename',
      },
      messages: {
        message: 'Presenter filename must match class name (e.g., FooPresenter.ts contains class FooPresenter) - see apps/website/lib/contracts/presenters/Presenter.ts',
      },
    },
    create(context) {
      return {
        ClassDeclaration(node) {
          const filename = context.getFilename();
          if (filename.includes('/lib/presenters/') && filename.endsWith('.ts')) {
            const expectedClassName = filename.split('/').pop().replace('.ts', '');
            const actualClassName = node.id?.name;
            
            if (actualClassName && actualClassName !== expectedClassName) {
              context.report({
                node,
                messageId: 'message',
              });
            }
          }
        },
      };
    },
  },

  // Rule 2: Service filename must match function name
  'service-filename-must-match-function': {
    meta: {
      type: 'problem',
      docs: {
        description: 'Enforce service filename matches function name',
        category: 'Filename',
      },
      messages: {
        message: 'Service filename must match function name (e.g., getUser.ts contains function getUser) - see apps/website/lib/contracts/services/Service.ts',
      },
    },
    create(context) {
      return {
        FunctionDeclaration(node) {
          const filename = context.getFilename();
          if (filename.includes('/lib/services/') && filename.endsWith('.ts')) {
            const expectedClassName = filename.split('/').pop().replace('.ts', '');
            const actualClassName = node.id?.name;
            
            if (actualClassName && actualClassName !== expectedClassName) {
              context.report({
                node,
                messageId: 'message',
              });
            }
          }
        },
      };
    },
  },

  // Rule 3: Display filename must end with Display.tsx and match class name
  'display-filename-must-end-with-display-tsx': {
    meta: {
      type: 'problem',
      docs: {
        description: 'Enforce display filename ends with Display.tsx and matches class name',
        category: 'Filename',
      },
      messages: {
        message: 'Display filenames must end with Display.tsx and the class name must match (e.g., RatingDisplay.tsx contains class RatingDisplay). Displays must be reusable, not screen-specific.',
      },
    },
    create(context) {
      return {
        ClassDeclaration(node) {
          const filename = context.getFilename();
          if (filename.includes('/lib/display-objects/') && filename.endsWith('.tsx')) {
            // Check if filename ends with Display.tsx
            if (!filename.endsWith('Display.tsx')) {
              context.report({
                node,
                messageId: 'message',
              });
              return;
            }
            
            // Extract expected class name from filename (remove path and Display.tsx suffix)
            const filenameOnly = filename.split('/').pop();
            const expectedClassName = filenameOnly.replace('.tsx', '');
            const actualClassName = node.id?.name;
            
            if (actualClassName && actualClassName !== expectedClassName) {
              context.report({
                node,
                messageId: 'message',
              });
            }
          }
        },
      };
    },
  },
};