import { Controller, Post, Get, Delete, Param, Body, Request, UseGuards } from '@nestjs/common';
import { ImportTaskService } from './import-task.service';
import { CombinedAuthGuard } from '../auth/combined-auth.guard';
import { RolesGuard } from '../auth/roles.guard';
import { Roles } from '../auth/roles.decorator';
import { UserRole } from '../user/user-role.enum';

@Controller('import-tasks')
@UseGuards(CombinedAuthGuard, RolesGuard)
export class ImportTaskController {
    constructor(private readonly taskService: ImportTaskService) { }

    @Post()
    @Roles(UserRole.TENANT_ADMIN, UserRole.SUPER_ADMIN)
    async create(@Request() req, @Body() body: any) {
        return this.taskService.create({
            sourcePath: body.sourcePath,
            targetGroupId: body.targetGroupId,
            targetGroupName: body.targetGroupName,
            embeddingModelId: body.embeddingModelId,
            scheduledAt: body.scheduledAt ? new Date(body.scheduledAt) : undefined,
            chunkSize: body.chunkSize,
            chunkOverlap: body.chunkOverlap,
            mode: body.mode,
            useHierarchy: body.useHierarchy ?? false,
            userId: req.user.id,
            tenantId: req.user.tenantId,
        });
    }

    @Get()
    async findAll(@Request() req) {
        return this.taskService.findAll(req.user.id);
    }

    @Delete(':id')
    async delete(@Param('id') id: string, @Request() req) {
        return this.taskService.delete(id, req.user.id);
    }
}
