import { Body, Controller, Delete, Param, Post, Put } from '@nestjs/common'
import { Org } from './entities/org.entity'
import { PageRequest } from '../common/dto/page-request'
import { OrgService } from './org.service'
import { HasAnyRoles, HasRoles } from '../auth/roles.decorator'
import { Role } from '../model/role.enum'
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
import { OrgUser } from './entities/org-user.entity'

@ApiTags('org.admin')
@Controller('/admin/org')
@ApiBearerAuth()
@HasAnyRoles(Role.Admin, Role.Org)
export class OrgAdminController {
    constructor(private readonly orgService: OrgService) {}

    @Post('/users')
    async orgUsers(@Body() page: PageRequest<OrgUser>) {
        return await this.orgService.findAllUsers(page)
    }

    @Put('/users')
    async addUsers(@Body() orgUsers: OrgUser[]) {
        return await this.orgService.addUsers(orgUsers)
    }

    @Delete('/users/:id')
    async removeUser(@Param('id') id) {
        return await this.orgService.removeUser(id)
    }

    @Post()
    async list(@Body() page: PageRequest<Org>) {
        return await this.orgService.findAll(page)
    }

    @Put()
    async create(@Body() org: Org) {
        return await this.orgService.create(org)
    }

    @Put('/:orgId')
    async update(@Body() org: Org) {
        return await this.orgService.update(org)
    }
}
