// SCB Admin Console - FI Controls Service
// FI approval/suspension, API profiles, limits

import prisma from '@/shared/database/prisma';
import { logger } from '@/infrastructure/monitoring/logger';
import { adminAuditService } from '@/core/admin/shared/admin-audit.service';
import { AdminPermission } from '@/core/admin/shared/permissions.constants';

export interface FIApprovalRequest {
  fiId: string;
  action: 'approve' | 'suspend';
  reason: string;
}

export interface FILimitsUpdate {
  fiId: string;
  limits: {
    byAssetType?: Record<string, number>;
    byCorridor?: Record<string, number>;
    dailyLimit?: number;
  };
}

export interface APIProfileAssignment {
  fiId: string;
  profileId: string;
  endpoints: string[];
}

export class FIControlsService {
  /**
   * Approve or suspend FI
   */
  async approveSuspendFI(
    employeeId: string,
    scbId: string,
    request: FIApprovalRequest
  ): Promise<{ success: boolean }> {
    await adminAuditService.logAction({
      employeeId,
      action: request.action === 'approve' ? 'approve_fi' : 'suspend_fi',
      permission: AdminPermission.FI_APPROVE_SUSPEND,
      resourceType: 'financial_institution',
      resourceId: request.fiId,
      metadata: { scbId, ...request },
    });

    // Would update FI status
    logger.info('FI approval/suspension', {
      employeeId,
      scbId,
      request,
    });

    return { success: true };
  }

  /**
   * Set FI limits
   */
  async setFILimits(
    employeeId: string,
    scbId: string,
    update: FILimitsUpdate
  ): Promise<{ success: boolean }> {
    await adminAuditService.logAction({
      employeeId,
      action: 'set_fi_limits',
      permission: AdminPermission.FI_SET_LIMITS,
      resourceType: 'financial_institution',
      resourceId: update.fiId,
      metadata: { scbId, ...update },
    });

    logger.info('FI limits updated', {
      employeeId,
      scbId,
      update,
    });

    return { success: true };
  }

  /**
   * Assign API profile to FI
   */
  async assignAPIProfile(
    employeeId: string,
    scbId: string,
    assignment: APIProfileAssignment
  ): Promise<{ success: boolean }> {
    await adminAuditService.logAction({
      employeeId,
      action: 'assign_api_profile',
      permission: AdminPermission.FI_API_PROFILES,
      resourceType: 'financial_institution',
      resourceId: assignment.fiId,
      metadata: { scbId, ...assignment },
    });

    logger.info('API profile assigned', {
      employeeId,
      scbId,
      assignment,
    });

    return { success: true };
  }
}

export const fiControlsService = new FIControlsService();

