import { AuthMethod, MultiTenancyInterceptor, UseAuth } from '@backend/auth'
import { Body, Controller, Get, HttpCode, Param, ParseUUIDPipe, Post, UseInterceptors } from '@nestjs/common'
import {
	ApiBadRequestResponse,
	ApiConflictResponse,
	ApiInternalServerErrorResponse,
	ApiNoContentResponse,
	ApiNotFoundResponse,
	ApiOkResponse,
	ApiTags,
} from '@nestjs/swagger'
import { TenantService } from '../services'
import { CompanyResponse, CreateTenantWithUserRequest } from '../types'

@Controller('tenant')
@ApiTags('default')
export class TenantController {
	constructor(private readonly _tenantService: TenantService) {}

	@Post()
	@HttpCode(204)
	@ApiNoContentResponse()
	@ApiBadRequestResponse()
	@ApiConflictResponse()
	@ApiInternalServerErrorResponse()
	async createTenant(@Body() request: CreateTenantWithUserRequest): Promise<void> {
		return this._tenantService.create(request)
	}

	@UseAuth(AuthMethod.JWT)
	@UseInterceptors(MultiTenancyInterceptor)
	@Get('/:id')
	@ApiOkResponse({ type: CompanyResponse })
	@ApiNotFoundResponse()
	@ApiInternalServerErrorResponse()
	async findById(@Param('id', ParseUUIDPipe) id: string): Promise<CompanyResponse> {
		return this._tenantService.findById(id)
	}
}
