import { prisma } from '../config/database';
import { Prisma } from '@prisma/client';
import { config } from '../config';
import { logger } from '../config/logger';
import jwt from 'jsonwebtoken';
import { createCategory, createCategoryResponse, deleteCategoryResponse, updateCategory, updateCategoryResponse, ListCategoriesResponse, Category } from '../types/categories.type';
import { UnauthorizedError, ValidationError, ForbiddenError } from '../middleware/errorHandler';

export class CategoryService {
    async listCategories(): Promise<ListCategoriesResponse> {
        try {
            const categories = await prisma.category.findMany();
            return {
                code: 200,
                message: 'Categories fetched successfully',
                data: {
                    categories: categories as unknown as Category[],
                    total: categories.length
                }
            };
        } catch (error) {
            logger.error('Failed to list categories', { error });
            throw error;
        }
    }

    async createCategory(token: string, category: createCategory): Promise<createCategoryResponse> {
        try {
            const decoded = jwt.verify(token, config.jwt.secret) as { id: number; username: string; email: string; roleCode: string };
            const newCategory = await prisma.category.create({
                data: category
            });
            return {
                code: 200,
                message: 'Category created successfully',
                data: {
                    id: newCategory.id,
                    name: newCategory.name as { th: string; en: string },
                    slug: newCategory.slug,
                    description: newCategory.description as { th: string; en: string },
                    created_by: decoded.id,
                }
            };
        } catch (error) {
            logger.error('Failed to create category', { error });
            throw error;
        }
    }

    async updateCategory(token: string, id: number, category: updateCategory): Promise<updateCategoryResponse> {
        try {
            const decoded = jwt.verify(token, config.jwt.secret) as { id: number; username: string; email: string; roleCode: string };
            const updatedCategory = await prisma.category.update({
                where: { id },
                data: category
            });
            return {
                code: 200,
                message: 'Category updated successfully',
                data: {
                    id: updatedCategory.id,
                    name: updatedCategory.name as { th: string; en: string },
                    slug: updatedCategory.slug,
                    description: updatedCategory.description as { th: string; en: string },
                    updated_by: decoded.id,
                }
            };
        } catch (error) {
            logger.error('Failed to update category', { error });
            throw error;
        }
    }

    async deleteCategory(id: number): Promise<deleteCategoryResponse> {
        try {
            const deletedCategory = await prisma.category.delete({
                where: { id }
            });
            return {
                code: 200,
                message: 'Category deleted successfully',
            };
        } catch (error) {
            logger.error('Failed to delete category', { error });
            throw error;
        }
    }
}
