import { PrismaService } from 'src/database/prisma.service';
import { AbilityAction } from '../ability.action';
import { AppAbility } from '../ability.factory';
import { IAbilityHandler } from '../interfaces/ability-handler.interface';
import {
  ExecutionContext,
  Injectable,
  NotFoundException,
} from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { CommentThreadWhereInput } from 'src/core/@generated/comment-thread/comment-thread-where.input';
import { assert } from 'src/utils/assert';
import { subject } from '@casl/ability';

class CommentThreadArgs {
  where?: CommentThreadWhereInput;
}

@Injectable()
export class ManageCommentThreadAbilityHandler implements IAbilityHandler {
  async handle(ability: AppAbility) {
    return ability.can(AbilityAction.Manage, 'CommentThread');
  }
}

@Injectable()
export class ReadCommentThreadAbilityHandler implements IAbilityHandler {
  handle(ability: AppAbility) {
    return ability.can(AbilityAction.Read, 'CommentThread');
  }
}

@Injectable()
export class CreateCommentThreadAbilityHandler implements IAbilityHandler {
  handle(ability: AppAbility) {
    return ability.can(AbilityAction.Create, 'CommentThread');
  }
}

@Injectable()
export class UpdateCommentThreadAbilityHandler implements IAbilityHandler {
  constructor(private readonly prismaService: PrismaService) {}

  async handle(ability: AppAbility, context: ExecutionContext) {
    const gqlContext = GqlExecutionContext.create(context);
    const args = gqlContext.getArgs<CommentThreadArgs>();
    const commentThread = await this.prismaService.commentThread.findFirst({
      where: args.where,
    });
    assert(commentThread, '', NotFoundException);

    return ability.can(
      AbilityAction.Update,
      subject('CommentThread', commentThread),
    );
  }
}

@Injectable()
export class DeleteCommentThreadAbilityHandler implements IAbilityHandler {
  constructor(private readonly prismaService: PrismaService) {}

  async handle(ability: AppAbility, context: ExecutionContext) {
    const gqlContext = GqlExecutionContext.create(context);
    const args = gqlContext.getArgs<CommentThreadArgs>();
    const commentThread = await this.prismaService.commentThread.findFirst({
      where: args.where,
    });
    assert(commentThread, '', NotFoundException);

    return ability.can(
      AbilityAction.Delete,
      subject('CommentThread', commentThread),
    );
  }
}
