import { Injectable, NotFoundException, ConflictException, Logger } from "@nestjs/common";
import { PrismaService } from "../prisma/prisma.service";
import { CreatePersonalityDto, UpdatePersonalityDto } from "./dto";
import { Personality } from "./entities/personality.entity";

/**
 * Service for managing personality/assistant configurations
 */
@Injectable()
export class PersonalitiesService {
  private readonly logger = new Logger(PersonalitiesService.name);

  constructor(private readonly prisma: PrismaService) {}

  /**
   * Create a new personality
   */
  async create(workspaceId: string, dto: CreatePersonalityDto): Promise<Personality> {
    // Check for duplicate name
    const existing = await this.prisma.personality.findFirst({
      where: { workspaceId, name: dto.name },
    });

    if (existing) {
      throw new ConflictException(`Personality with name "${dto.name}" already exists`);
    }

    // If creating a default personality, unset other defaults
    if (dto.isDefault) {
      await this.unsetOtherDefaults(workspaceId);
    }

    const personality = await this.prisma.personality.create({
      data: {
        workspaceId,
        name: dto.name,
        displayName: dto.displayName,
        description: dto.description ?? null,
        systemPrompt: dto.systemPrompt,
        temperature: dto.temperature ?? null,
        maxTokens: dto.maxTokens ?? null,
        llmProviderInstanceId: dto.llmProviderInstanceId ?? null,
        isDefault: dto.isDefault ?? false,
        isEnabled: dto.isEnabled ?? true,
      },
    });

    this.logger.log(`Created personality ${personality.id} for workspace ${workspaceId}`);
    return personality;
  }

  /**
   * Find all personalities for a workspace
   */
  async findAll(workspaceId: string): Promise<Personality[]> {
    return this.prisma.personality.findMany({
      where: { workspaceId },
      orderBy: [{ isDefault: "desc" }, { name: "asc" }],
    });
  }

  /**
   * Find a specific personality by ID
   */
  async findOne(workspaceId: string, id: string): Promise<Personality> {
    const personality = await this.prisma.personality.findUnique({
      where: { id, workspaceId },
    });

    if (!personality) {
      throw new NotFoundException(`Personality with ID ${id} not found`);
    }

    return personality;
  }

  /**
   * Find a personality by name
   */
  async findByName(workspaceId: string, name: string): Promise<Personality> {
    const personality = await this.prisma.personality.findFirst({
      where: { workspaceId, name },
    });

    if (!personality) {
      throw new NotFoundException(`Personality with name "${name}" not found`);
    }

    return personality;
  }

  /**
   * Find the default personality for a workspace
   */
  async findDefault(workspaceId: string): Promise<Personality> {
    const personality = await this.prisma.personality.findFirst({
      where: { workspaceId, isDefault: true, isEnabled: true },
    });

    if (!personality) {
      throw new NotFoundException(`No default personality found for workspace ${workspaceId}`);
    }

    return personality;
  }

  /**
   * Update an existing personality
   */
  async update(workspaceId: string, id: string, dto: UpdatePersonalityDto): Promise<Personality> {
    // Check existence
    await this.findOne(workspaceId, id);

    // Check for duplicate name if updating name
    if (dto.name) {
      const existing = await this.prisma.personality.findFirst({
        where: { workspaceId, name: dto.name, id: { not: id } },
      });

      if (existing) {
        throw new ConflictException(`Personality with name "${dto.name}" already exists`);
      }
    }

    // If setting as default, unset other defaults
    if (dto.isDefault === true) {
      await this.unsetOtherDefaults(workspaceId, id);
    }

    const personality = await this.prisma.personality.update({
      where: { id },
      data: dto,
    });

    this.logger.log(`Updated personality ${id} for workspace ${workspaceId}`);
    return personality;
  }

  /**
   * Delete a personality
   */
  async delete(workspaceId: string, id: string): Promise<void> {
    // Check existence
    await this.findOne(workspaceId, id);

    await this.prisma.personality.delete({
      where: { id },
    });

    this.logger.log(`Deleted personality ${id} from workspace ${workspaceId}`);
  }

  /**
   * Set a personality as the default
   */
  async setDefault(workspaceId: string, id: string): Promise<Personality> {
    // Check existence
    await this.findOne(workspaceId, id);

    // Unset other defaults
    await this.unsetOtherDefaults(workspaceId, id);

    // Set this one as default
    const personality = await this.prisma.personality.update({
      where: { id },
      data: { isDefault: true },
    });

    this.logger.log(`Set personality ${id} as default for workspace ${workspaceId}`);
    return personality;
  }

  /**
   * Unset the default flag on all other personalities in the workspace
   */
  private async unsetOtherDefaults(workspaceId: string, excludeId?: string): Promise<void> {
    const currentDefault = await this.prisma.personality.findFirst({
      where: {
        workspaceId,
        isDefault: true,
        ...(excludeId && { id: { not: excludeId } }),
      },
    });

    if (currentDefault) {
      await this.prisma.personality.update({
        where: { id: currentDefault.id },
        data: { isDefault: false },
      });
    }
  }
}
