import { Body, Controller, Delete, Get, Param, Post, Put, Query } from '@nestjs/common'
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
import { FormService } from './form.service'
import { PageRequest } from 'src/common/dto/page-request'
import { Form } from './entities/form.entity'
import { FormDto } from './dto/form.dto'
import { Field } from './entities/filed.entity'
import { UserField } from './entities/userField.entity'
import { number } from 'yup'

@ApiTags('form')
@ApiBearerAuth()
@Controller('/form')
export class FormController {
    constructor(private readonly formService: FormService) {}

    @Post('/base')
    public async list(@Body() page: PageRequest<Form>) {
        return await this.formService.findAll(page)
    }

    @Put('/base')
    public async create(@Body() form: Partial<Form>) {
        return await this.formService.save(form)
    }

    @Put('/base/:id')
    public async update(@Body() form: Partial<Form>) {
        return await this.formService.update(form)
    }

    @Post('/fileds')
    public async findAllFileds(@Body() page: PageRequest<Field>) {
        return await this.formService.findAllFileds(page)
    }

    @Delete('/base/:id')
    public async delete(@Param('id') id: string) {
        return await this.formService.delete(Number(id))
    }

    @Put('/createFiled')
    public async createFiled(@Body() field: Partial<Field>) {
        return await this.formService.saveFiled(field)
    }

    @Put('/updateFiled/:id')
    public async updateFiled(@Body() field: Partial<Field>) {
        return await this.formService.updateFiled(field)
    }

    @Post('/userFileds')
    public async createFileds(@Body() fileds: Array<Partial<UserField>>) {
        return await this.formService.saveUserFileds(fileds)
    }

    @Get('/getUserFields')
    public async getUserFields(@Query() { userId, orgId }) {

        return await this.formService.getUserFields(userId, orgId)
    }

    @Get('/getUserFieldsByForm')
    public async getUserFieldsByForm(@Query() { formId }) {

        return await this.formService.getUserFieldsByForm(formId)
    }
}
