import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import { TEMPLATE_SEEDS, type TemplateSeed } from './seeds';

/**
 * Seeds platform-default templates (scopeId NULL) from the repo files on boot — DB is runtime truth,
 * files are the version-controlled source. Idempotent: a seed is inserted only if its exact
 * (key, channel, locale, version) default is absent, so re-boots never duplicate and a bumped
 * version adds a new row while leaving the old one for audit/replay.
 */
@Injectable()
export class TemplateSeeder implements OnModuleInit {
  private readonly log = new Logger(TemplateSeeder.name);

  constructor(private readonly prisma: PrismaService) {}

  async onModuleInit(): Promise<void> {
    const inserted = await this.seed(TEMPLATE_SEEDS);
    if (inserted > 0) this.log.log(`seeded ${inserted} default template(s)`);
  }

  /** Returns how many rows were newly inserted. */
  async seed(seeds: TemplateSeed[]): Promise<number> {
    let inserted = 0;
    for (const s of seeds) {
      const exists = await this.prisma.iiosMessageTemplate.findFirst({
        where: { scopeId: null, key: s.key, channel: s.channel, locale: s.locale, version: s.version },
        select: { id: true },
      });
      if (exists) continue;
      await this.prisma.iiosMessageTemplate.create({
        data: {
          scopeId: null,
          key: s.key,
          channel: s.channel,
          locale: s.locale,
          version: s.version,
          subject: s.subject,
          bodyHtml: s.bodyHtml,
          bodyText: s.bodyText,
          variables: s.variables as Prisma.InputJsonValue,
        },
      });
      inserted++;
    }
    return inserted;
  }
}
