import * as plugins from '../../plugins.js';
import type { OpsServer } from '../classes.opsserver.js';
import * as interfaces from '../../../ts_interfaces/index.js';
import { requireOpsAuth } from '../helpers/auth.js';

/**
 * CRUD + DNS provisioning handler for email domains.
 *
 * Auth: admin JWT or API token with `email-domains:read` / `email-domains:write` scope.
 */
export class EmailDomainHandler {
  public typedrouter = new plugins.typedrequest.TypedRouter();

  constructor(private opsServerRef: OpsServer) {
    this.opsServerRef.typedrouter.addTypedRouter(this.typedrouter);
    this.registerHandlers();
  }

  private async requireAuth(
    request: { identity?: interfaces.data.IIdentity; apiToken?: string },
    requiredScope?: interfaces.data.TApiTokenScope,
  ): Promise<string> {
    const auth = await requireOpsAuth(this.opsServerRef, request, {
      scope: requiredScope,
      requireAdminIdentity: requiredScope?.endsWith(':write'),
    });
    return auth.userId;
  }

  private get manager() {
    return this.opsServerRef.dcRouterRef.emailDomainManager;
  }

  private registerHandlers(): void {
    // List all email domains
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetEmailDomains>(
        'getEmailDomains',
        async (dataArg) => {
          await this.requireAuth(dataArg, 'email-domains:read' as any);
          if (!this.manager) return { domains: [] };
          return { domains: await this.manager.getAll() };
        },
      ),
    );

    // Get single email domain
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetEmailDomain>(
        'getEmailDomain',
        async (dataArg) => {
          await this.requireAuth(dataArg, 'email-domains:read' as any);
          if (!this.manager) return { domain: null };
          return { domain: await this.manager.getById(dataArg.id) };
        },
      ),
    );

    // Create email domain
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_CreateEmailDomain>(
        'createEmailDomain',
        async (dataArg) => {
          await this.requireAuth(dataArg, 'email-domains:write' as any);
          if (!this.manager) {
            return { success: false, message: 'EmailDomainManager not initialized' };
          }
          try {
            const domain = await this.manager.createEmailDomain({
              linkedDomainId: dataArg.linkedDomainId,
              subdomain: dataArg.subdomain,
              dkimSelector: dataArg.dkimSelector,
              dkimKeySize: dataArg.dkimKeySize,
              rotateKeys: dataArg.rotateKeys,
              rotationIntervalDays: dataArg.rotationIntervalDays,
            });
            return { success: true, domain };
          } catch (err: unknown) {
            return { success: false, message: (err as Error).message };
          }
        },
      ),
    );

    // Update email domain
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_UpdateEmailDomain>(
        'updateEmailDomain',
        async (dataArg) => {
          await this.requireAuth(dataArg, 'email-domains:write' as any);
          if (!this.manager) {
            return { success: false, message: 'EmailDomainManager not initialized' };
          }
          try {
            await this.manager.updateEmailDomain(dataArg.id, {
              rotateKeys: dataArg.rotateKeys,
              rotationIntervalDays: dataArg.rotationIntervalDays,
              rateLimits: dataArg.rateLimits,
            });
            return { success: true };
          } catch (err: unknown) {
            return { success: false, message: (err as Error).message };
          }
        },
      ),
    );

    // Delete email domain
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_DeleteEmailDomain>(
        'deleteEmailDomain',
        async (dataArg) => {
          await this.requireAuth(dataArg, 'email-domains:write' as any);
          if (!this.manager) {
            return { success: false, message: 'EmailDomainManager not initialized' };
          }
          try {
            await this.manager.deleteEmailDomain(dataArg.id);
            return { success: true };
          } catch (err: unknown) {
            return { success: false, message: (err as Error).message };
          }
        },
      ),
    );

    // Validate DNS records
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_ValidateEmailDomain>(
        'validateEmailDomain',
        async (dataArg) => {
          await this.requireAuth(dataArg, 'email-domains:read' as any);
          if (!this.manager) {
            return { success: false, message: 'EmailDomainManager not initialized' };
          }
          try {
            const records = await this.manager.validateDns(dataArg.id);
            const domain = await this.manager.getById(dataArg.id);
            return { success: true, domain: domain ?? undefined, records };
          } catch (err: unknown) {
            return { success: false, message: (err as Error).message };
          }
        },
      ),
    );

    // Get required DNS records
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_GetEmailDomainDnsRecords>(
        'getEmailDomainDnsRecords',
        async (dataArg) => {
          await this.requireAuth(dataArg, 'email-domains:read' as any);
          if (!this.manager) return { records: [] };
          return { records: await this.manager.getRequiredDnsRecords(dataArg.id) };
        },
      ),
    );

    // Auto-provision DNS records
    this.typedrouter.addTypedHandler(
      new plugins.typedrequest.TypedHandler<interfaces.requests.IReq_ProvisionEmailDomainDns>(
        'provisionEmailDomainDns',
        async (dataArg) => {
          await this.requireAuth(dataArg, 'email-domains:write' as any);
          if (!this.manager) {
            return { success: false, message: 'EmailDomainManager not initialized' };
          }
          try {
            const provisioned = await this.manager.provisionDnsRecords(dataArg.id);
            return { success: true, provisioned };
          } catch (err: unknown) {
            return { success: false, message: (err as Error).message };
          }
        },
      ),
    );
  }
}
