import {
  Injectable,
  NotFoundException,
  ForbiddenException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { AssessmentTemplate } from '../entities/assessment-template.entity';
import { CreateTemplateDto } from '../dto/create-template.dto';
import { UpdateTemplateDto } from '../dto/update-template.dto';
import { TenantService } from '../../tenant/tenant.service';

@Injectable()
export class TemplateService {
  constructor(
    @InjectRepository(AssessmentTemplate)
    private readonly templateRepository: Repository<AssessmentTemplate>,
    private readonly tenantService: TenantService,
  ) {}

  async create(
    createDto: CreateTemplateDto,
    userId: string,
    tenantId: string,
  ): Promise<AssessmentTemplate> {
    const { ...data } = createDto;
    const template = this.templateRepository.create({
      ...data,
      createdBy: userId,
      tenantId,
    });
    return this.templateRepository.save(template);
  }

  async findAll(tenantId: string): Promise<AssessmentTemplate[]> {
    return this.templateRepository.find({
      where: { tenantId, isActive: true },
      relations: ['knowledgeGroup'],
      order: { createdAt: 'DESC' },
    });
  }

  async findOne(
    id: string,
    userId: string,
    tenantId: string,
  ): Promise<AssessmentTemplate> {
    const template = await this.templateRepository.findOne({
      where: { id },
      relations: ['knowledgeGroup'],
    });

    if (!template) {
      throw new NotFoundException(`Template with ID "${id}" not found`);
    }

    // Check permission using TenantService
    const hasAccess = await this.tenantService.canAccessTenant(
      userId,
      template.tenantId,
      tenantId,
    );
    if (!hasAccess) {
      throw new ForbiddenException(
        `You do not have permission to access this template`,
      );
    }

    return template;
  }

  async update(
    id: string,
    updateDto: UpdateTemplateDto,
    userId: string,
    tenantId: string,
  ): Promise<AssessmentTemplate> {
    const template = await this.findOne(id, userId, tenantId);
    Object.assign(template, updateDto);
    return this.templateRepository.save(template);
  }

  async remove(id: string, userId: string, tenantId: string): Promise<void> {
    const template = await this.findOne(id, userId, tenantId);
    // Soft delete by setting isActive to false
    template.isActive = false;
    await this.templateRepository.save(template);
  }
}
