import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe, ParseBoolPipe, Query, DefaultValuePipe, ParseArrayPipe } from '@nestjs/common';
import { JobsService } from './jobs.service';
import { CreateJobDto } from './dto/create-job.dto';
import { UpdateJobDto } from './dto/update-job.dto';
import { ApiTags } from '@nestjs/swagger';

@Controller('jobs')
@ApiTags('jobs')
export class JobsController {
	constructor(private readonly jobsService: JobsService) {}

	@Post()
	create(@Body() createJobDto: CreateJobDto) {
		return this.jobsService.create(createJobDto);
	}

	@Get()
	findAll(@Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number, @Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit: number, @Query('sortBy') sortBy: string, @Query('sortOrder') sortOrder: string, @Query('search') search: string, @Query('status', new DefaultValuePipe([]), ParseArrayPipe) status: string | Array<string> | undefined, @Query('assignee', new DefaultValuePipe(0), ParseIntPipe) assignee: number | undefined) {
		return this.jobsService.findAll((page - 1) * limit, limit, sortBy, sortOrder, search, status, assignee);
	}

	@Get('/simpleTotal')
	findAllSimpleTotal(@Query('client') client: string, @Query('status', new DefaultValuePipe([]), ParseArrayPipe) status: string | Array<string> | undefined) {
		return this.jobsService.findAllSimpleTotal(client, status);
	}

	@Get('/search')
	findBySearch(@Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number, @Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit: number, @Query('sortBy') sortBy: string, @Query('sortOrder') sortOrder: string, @Query('search') search: string, @Query('searchType') searchType: string) {
		return this.jobsService.findBySearch((page - 1) * limit, limit, sortBy, sortOrder, search, searchType);
	}

	@Get('/stockTake/:jobId')
	createStockTake(@Param('jobId', ParseIntPipe) jobId: number) {
		return this.jobsService.generateStockTake(jobId);
	}

	@Get('/query')
	findByMultiSearch(@Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number, @Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit: number, @Query('sortBy') sortBy: string, @Query('sortOrder') sortOrder: string, @Query('includeTotals', new DefaultValuePipe(true), ParseBoolPipe) includeTotals: boolean | undefined, @Query('search') search: string) {
		return this.jobsService.findByMultiSearch((page - 1) * limit, limit, sortBy, sortOrder, includeTotals, search);
	}

	@Get(':id')
	findOne(@Param('id') id: string, @Query('fields', new DefaultValuePipe([]), ParseArrayPipe) fields: Array<string> | undefined) {
		return this.jobsService.findOne(+id, fields);
	}

	@Get('dates/:startDate/:endDate/:status?')
	findMany(@Param('startDate') startDate: string, @Param('endDate') endDate: string, @Param('status') status: string) {
		return this.jobsService.findManyDates(startDate, endDate, status);
	}

	@Patch(':id')
	update(@Param('id') id: string, @Body() updateJobDto: UpdateJobDto) {
		return this.jobsService.update(+id, updateJobDto);
	}

	@Delete(':id')
	remove(@Param('id') id: string) {
		return this.jobsService.remove(+id);
	}
}
