// Token Class Manager Service
// Manage token class definitions
// Supplement B: MetaverseDubai Integration Framework

import prisma from '@/shared/database/prisma';
import { v4 as uuidv4 } from 'uuid';
import { logger } from '@/infrastructure/monitoring/logger';


export interface CreateTokenClassRequest {
  tokenClass:
    | 'virtual_land'
    | 'avatar_asset'
    | 'business_license'
    | 'event_rights'
    | 'data_ownership'
    | 'ai_companion';
  className: string;
  description: string;
  metadata?: Record<string, unknown>;
}

export interface TokenClassResult {
  tokenClassId: string;
  tokenClass: string;
  status: string;
}

export class TokenClassManagerService {
  /**
   * Create token class definition
   */
  async createTokenClass(
    request: CreateTokenClassRequest
  ): Promise<TokenClassResult> {
    logger.info('Token Class Manager: Creating token class', { request });

    const tokenClassId = `TOKEN-CLASS-${uuidv4()}`;

    const tokenClass = await prisma.metaverse_token_classes.create({
      data: {
        tokenClassId,
        tokenClass: request.tokenClass,
        className: request.className,
        description: request.description,
        metadata: request.metadata || {},
        status: 'active',
      },
    });

    logger.info('Token Class Manager: Token class created', { tokenClassId });

    return {
      tokenClassId: tokenClass.tokenClassId,
      tokenClass: tokenClass.tokenClass,
      status: tokenClass.status,
    };
  }

  /**
   * Get token class by ID
   */
  async getTokenClass(tokenClass: string) {
    return await prisma.metaverse_token_classes.findUnique({
      where: { tokenClass },
    });
  }

  /**
   * Get all active token classes
   */
  async getAllTokenClasses() {
    return await prisma.metaverse_token_classes.findMany({
      where: { status: 'active' },
      orderBy: { className: 'asc' },
    });
  }

  /**
   * Initialize default token classes
   */
  async initializeDefaultTokenClasses(): Promise<void> {
    const defaultClasses: CreateTokenClassRequest[] = [
      {
        tokenClass: 'virtual_land',
        className: 'Virtual Land',
        description: 'Tokenized virtual land parcels in metaverse',
      },
      {
        tokenClass: 'avatar_asset',
        className: 'Avatar Asset',
        description: 'Avatar clothing, tools, abilities, and accessories',
      },
      {
        tokenClass: 'business_license',
        className: 'Business License',
        description: 'Business licenses for metaverse commerce',
      },
      {
        tokenClass: 'event_rights',
        className: 'Event Rights',
        description: 'Rights to host or attend metaverse events',
      },
      {
        tokenClass: 'data_ownership',
        className: 'Data Ownership Token',
        description: 'Tokens representing ownership of data assets',
      },
      {
        tokenClass: 'ai_companion',
        className: 'AI Companion Ownership',
        description: 'Ownership tokens for AI companions in metaverse',
      },
    ];

    for (const tokenClass of defaultClasses) {
      const existing = await this.getTokenClass(tokenClass.tokenClass);
      if (!existing) {
        await this.createTokenClass(tokenClass);
      }
    }

    logger.info('Token Class Manager: Default token classes initialized');
  }
}

export const tokenClassManagerService = new TokenClassManagerService();

