import { Module, OnModuleInit } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Organization } from '../entities/organization.entity';
import { OrganizationsService } from './organizations.service';
import { OrganizationsController } from './organizations.controller';
import { OperationLogsModule } from '../operation-logs/operation-logs.module';

@Module({
  imports: [TypeOrmModule.forFeature([Organization]), OperationLogsModule],
  controllers: [OrganizationsController],
  providers: [OrganizationsService],
  exports: [OrganizationsService],
})
export class OrganizationsModule implements OnModuleInit {
  constructor(private readonly service: OrganizationsService) {}

  async onModuleInit() {
    try {
      await this.service.getHostOrganization();
    } catch {
      await this.service.create({
        name: process.env.HOST_ORGANIZATION_NAME || '本机构',
        code: process.env.HOST_ORGANIZATION_CODE || 'HOST',
        isHost: true,
        color: '#1677ff',
        notes: '系统默认运营主体',
      });
    }
  }
}
