import { CoreEntity } from '@/entities/core/core-entity';
import { Body, Delete, Get, Param, Post, Put, Req } from '@nestjs/common';
import { Paginate, PaginateQuery } from 'nestjs-paginate';
import CoreService from './core-service';
import { BulkDelDto } from './dtos/bulk-delete';
import {
  FunctionPermission,
  HasFeature,
} from '@/modules/authorization/decorators/has-feature.decorator';
import { SystemLang } from '../lang/system.lang';
import { Request } from 'express';
import { RelationSchemaInput } from 'nestjs-paginate/lib/helper';

export default class CoreController<
  E extends CoreEntity,
  S extends CoreService<E>,
> {
  public static FEATURE_INDEX = {};

  constructor(
    protected readonly service: S,
    protected options?: { relations?: RelationSchemaInput<E> },
  ) {}

  @Get(':id')
  @HasFeature({
    function_permisstion: FunctionPermission.READ,
    name: SystemLang.getCustomText({
      vi: 'Xem chi tiết',
      en: 'View Details',
    }),
    description: SystemLang.getCustomText({
      vi: 'Cho phép xem chi tiết bản ghi theo ID',
      en: 'Allows viewing details of a record by ID',
    }),
  })
  async getById(@Param('id') id: E['id']) {
    return await this.service.getById(id, this.options?.relations);
  }

  @Post()
  @HasFeature({
    function_permisstion: FunctionPermission.CREATE,
    disabled: true,
    name: SystemLang.getCustomText({
      vi: 'Tạo mới',
      en: 'Create new',
    }),
    description: SystemLang.getCustomText({
      vi: 'Cho phép tạo mới bản ghi',
      en: 'Allows creating a new record',
    }),
  })
  async create(@Body() data: any) {
    return await this.service.create(data);
  }

  @Get()
  @HasFeature({
    function_permisstion: FunctionPermission.READ,
    name: SystemLang.getCustomText({
      vi: 'Xem danh sách',
      en: 'View List',
    }),
    description: SystemLang.getCustomText({
      vi: 'Cho phép xem danh sách bản ghi có phân trang',
      en: 'Allows viewing a paginated list of records',
    }),
  })
  async index(@Paginate() query: PaginateQuery) {
    return await this.service.index(query);
  }

  @Delete('bulk-delete')
  @HasFeature({
    function_permisstion: FunctionPermission.DELETE,
    name: SystemLang.getCustomText({
      vi: 'Xoá hàng loạt',
      en: 'Bulk Delete',
    }),
    description: SystemLang.getCustomText({
      vi: 'Cho phép xoá nhiều bản ghi cùng lúc',
      en: 'Allows deleting multiple records at once',
    }),
  })
  async bulkDelete(@Body() { ids }: BulkDelDto, @Req() resquest: Request) {
    return await this.service.bulkDelete(ids, resquest);
  }

  @Put('bulk-update')
  @HasFeature({
    function_permisstion: FunctionPermission.UPDATE,
    disabled: true,
    name: SystemLang.getCustomText({
      vi: 'Cập nhật hàng loạt',
      en: 'Bulk Update',
    }),
    description: SystemLang.getCustomText({
      vi: 'Cho phép cập nhật nhiều bản ghi cùng lúc',
      en: 'Allows updating multiple records at once',
    }),
  })
  async bulkUpdate(@Body() data: E[] | any, @Req() resquest: Request) {
    return await this.service.bulkUpdate(data, resquest);
  }

  @Put(':id')
  @HasFeature({
    function_permisstion: FunctionPermission.UPDATE,
    name: SystemLang.getCustomText({
      vi: 'Cập nhật',
      en: 'Update',
    }),
    description: SystemLang.getCustomText({
      vi: 'Cho phép cập nhật bản ghi',
      en: 'Allows updating records',
    }),
  })
  async update(
    @Body() data: E | any,
    @Param('id') id: E['id'],
    @Req() resquest: Request,
  ) {
    return await this.service.update(id, data, resquest);
  }

  @Delete(':id')
  @HasFeature({
    function_permisstion: FunctionPermission.DELETE,
    name: SystemLang.getCustomText({
      vi: 'Xoá bản ghi',
      en: 'Delete Record',
    }),
    description: SystemLang.getCustomText({
      vi: 'Cho phép xoá một bản ghi theo ID',
      en: 'Allows deleting a single record by ID',
    }),
  })
  async delete(@Param('id') id: E['id'], @Req() resquest: Request) {
    return await this.service.delete(id, resquest);
  }
}
