import { Get, Body, Post, Route, Tags, SuccessResponse, Response, Delete, Controller, Security, Request, Put, Path } from 'tsoa';
import { ValidationError } from '../middleware/errorHandler';
import { CategoryService } from '../services/categories.service';
import { createCategory, createCategoryResponse, deleteCategoryResponse, updateCategory, updateCategoryResponse, ListCategoriesResponse } from '../types/categories.type';

@Route('api/categories')
@Tags('Categories')
export class CategoriesController {
    private categoryService = new CategoryService();

    @Get()
    @SuccessResponse('200', 'Categories fetched successfully')
    @Response('401', 'Invalid or expired token')
    public async listCategories(): Promise<ListCategoriesResponse> {
        return await this.categoryService.listCategories();
    }
}

@Route('api/admin/categories')
@Tags('Admin/Categories')
export class CategoriesAdminController {
    private categoryService = new CategoryService();

    @Post()
    @Security('jwt', ['admin'])
    @SuccessResponse('200', 'Category created successfully')
    @Response('401', 'Invalid or expired token')
    public async createCategory(@Request() request: any, @Body() body: createCategory): Promise<createCategoryResponse> {
        const token = request.headers.authorization?.replace('Bearer ', '') || '';
        return await this.categoryService.createCategory(token, body);
    }

    @Put('{id}')
    @Security('jwt', ['admin'])
    @SuccessResponse('200', 'Category updated successfully')
    @Response('401', 'Invalid or expired token')
    public async updateCategory(@Request() request: any, @Body() body: updateCategory): Promise<updateCategoryResponse> {
        const token = request.headers.authorization?.replace('Bearer ', '') || '';
        return await this.categoryService.updateCategory(token, body.id, body);
    }

    @Delete('{id}')
    @Security('jwt', ['admin'])
    @SuccessResponse('200', 'Category deleted successfully')
    @Response('401', 'Invalid or expired token')
    public async deleteCategory(@Request() request: any, @Path() id: number): Promise<deleteCategoryResponse> {
        const token = request.headers.authorization?.replace('Bearer ', '') || '';
        return await this.categoryService.deleteCategory(id);
    }
}