import { Injectable } from '@nestjs/common';
import { MailProfileService } from '../../mail-profile/mail-profile.service';
import { McpTool } from '../decorators/mcp-tool.decorator';
import { McpContext } from '../types/mcp-context.type';

@Injectable()
export class CoreMailProfileMcpTools {
  constructor(private readonly mailProfileService: MailProfileService) {}

  @McpTool({
    name: 'core.mail-profile.list',
    description: 'Lists mail profiles with optional pagination. Admin only.',
    inputSchema: {
      type: 'object',
      properties: {
        page: { type: 'number', description: 'Page number (default: 1)' },
        pageSize: { type: 'number', description: 'Items per page (default: 20)' },
        search: { type: 'string', description: 'Search term' },
        sortField: { type: 'string', description: 'Field to sort by' },
        sortOrder: { type: 'string', enum: ['asc', 'desc'], description: 'Sort direction' },
      },
    },
    readOnly: true,
  })
  async listMailProfiles(
    args: { page?: number; pageSize?: number; search?: string; sortField?: string; sortOrder?: string },
    _context: McpContext,
  ): Promise<any> {
    return this.mailProfileService.list({
      page: args.page ?? 1,
      pageSize: args.pageSize ?? 20,
      search: args.search ?? '',
      sortField: args.sortField ?? 'id',
      sortOrder: (args.sortOrder as any) ?? 'asc',
      fields: '',
    });
  }

  @McpTool({
    name: 'core.mail-profile.get',
    description: 'Returns a single mail profile by ID. Admin only.',
    inputSchema: {
      type: 'object',
      properties: {
        id: { type: 'number', description: 'Mail profile ID' },
      },
      required: ['id'],
    },
    readOnly: true,
  })
  async getMailProfile(args: { id: number }, _context: McpContext): Promise<any> {
    return this.mailProfileService.get(args.id);
  }

  @McpTool({
    name: 'core.mail-profile.create',
    description: 'Creates a new mail profile (SMTP configuration). Admin only.',
    inputSchema: {
      type: 'object',
      properties: {
        name: { type: 'string', description: 'Profile name' },
        host: { type: 'string', description: 'SMTP host' },
        port: { type: 'number', description: 'SMTP port' },
        username: { type: 'string', description: 'SMTP username' },
        password: { type: 'string', description: 'SMTP password' },
      },
      required: ['name', 'host', 'port', 'username'],
    },
  })
  async createMailProfile(args: { name: string; host: string; port: number; username: string; password?: string }, _context: McpContext): Promise<any> {
    return this.mailProfileService.create(args as any);
  }

  @McpTool({
    name: 'core.mail-profile.update',
    description: 'Updates an existing mail profile by ID. Admin only.',
    inputSchema: {
      type: 'object',
      properties: {
        id: { type: 'number', description: 'Mail profile ID' },
        name: { type: 'string', description: 'New profile name' },
        host: { type: 'string', description: 'New SMTP host' },
        port: { type: 'number', description: 'New SMTP port' },
        username: { type: 'string', description: 'New SMTP username' },
        password: { type: 'string', description: 'New SMTP password' },
      },
      required: ['id'],
    },
  })
  async updateMailProfile(args: { id: number; [key: string]: any }, _context: McpContext): Promise<any> {
    const { id, ...data } = args;
    return this.mailProfileService.update({ id, data: data as any });
  }

  @McpTool({
    name: 'core.mail-profile.delete',
    description: 'Deletes one or more mail profiles by their IDs. Admin only.',
    inputSchema: {
      type: 'object',
      properties: {
        ids: { type: 'array', items: { type: 'number' }, description: 'Mail profile IDs to delete' },
      },
      required: ['ids'],
    },
  })
  async deleteMailProfiles(args: { ids: number[] }, _context: McpContext): Promise<any> {
    return this.mailProfileService.delete({ ids: args.ids });
  }

  @McpTool({
    name: 'core.mail-profile.test',
    description: 'Tests a mail profile by sending a test message. Admin only.',
    inputSchema: {
      type: 'object',
      properties: {
        name: { type: 'string', description: 'Profile name' },
        host: { type: 'string', description: 'SMTP host' },
        port: { type: 'number', description: 'SMTP port' },
        username: { type: 'string', description: 'SMTP username' },
        password: { type: 'string', description: 'SMTP password' },
      },
      required: ['host', 'port', 'username'],
    },
  })
  async testMailProfile(args: Record<string, any>, _context: McpContext): Promise<any> {
    return this.mailProfileService.test(args as any);
  }
}
