"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const asyncHandler_1 = require("../utils/asyncHandler");
const db_1 = require("../db");
const schema_1 = require("../db/schema");
const drizzle_orm_1 = require("drizzle-orm");
const auth_1 = require("../utils/auth");
const errorHandler_1 = require("../middleware/errorHandler");
const validation_1 = require("../utils/validation");
const router = (0, express_1.Router)();
router.use(auth_1.authMiddleware);
router.get('/', (0, asyncHandler_1.asyncHandler)(async (req, res) => {
    const userCategories = await db_1.db
        .select()
        .from(schema_1.categories)
        .where((0, drizzle_orm_1.eq)(schema_1.categories.userId, req.user.userId))
        .orderBy((0, drizzle_orm_1.asc)(schema_1.categories.order), (0, drizzle_orm_1.asc)(schema_1.categories.createdAt));
    res.json({ categories: userCategories });
}));
router.post('/', (0, asyncHandler_1.asyncHandler)(async (req, res) => {
    const data = validation_1.categoryCreateSchema.parse(req.body);
    const userId = req.user.userId;
    const now = Date.now();
    const maxOrder = await db_1.db
        .select({ order: schema_1.categories.order })
        .from(schema_1.categories)
        .where((0, drizzle_orm_1.eq)(schema_1.categories.userId, userId))
        .orderBy((0, drizzle_orm_1.desc)(schema_1.categories.order))
        .limit(1);
    const newCategory = {
        id: `cat_${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`,
        userId,
        name: data.name,
        color: data.color,
        order: data.order ?? (maxOrder[0]?.order ?? -1) + 1,
        createdAt: now,
        updatedAt: now,
    };
    await db_1.db.insert(schema_1.categories).values(newCategory);
    res.status(201).json(newCategory);
}));
router.patch('/:id', (0, asyncHandler_1.asyncHandler)(async (req, res) => {
    const data = validation_1.categoryUpdateSchema.parse(req.body);
    const userId = req.user.userId;
    const existing = await db_1.db
        .select()
        .from(schema_1.categories)
        .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(schema_1.categories.id, req.params.id), (0, drizzle_orm_1.eq)(schema_1.categories.userId, userId)))
        .limit(1);
    if (existing.length === 0) {
        throw new errorHandler_1.AppError('NOT_FOUND', 'Category not found', 404);
    }
    const now = Date.now();
    const updated = await db_1.db
        .update(schema_1.categories)
        .set({ ...data, updatedAt: now })
        .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(schema_1.categories.id, req.params.id), (0, drizzle_orm_1.eq)(schema_1.categories.userId, userId)))
        .returning();
    res.json(updated[0]);
}));
router.delete('/:id', (0, asyncHandler_1.asyncHandler)(async (req, res) => {
    const userId = req.user.userId;
    const existing = await db_1.db
        .select()
        .from(schema_1.categories)
        .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(schema_1.categories.id, req.params.id), (0, drizzle_orm_1.eq)(schema_1.categories.userId, userId)))
        .limit(1);
    if (existing.length === 0) {
        throw new errorHandler_1.AppError('NOT_FOUND', 'Category not found', 404);
    }
    await db_1.db
        .delete(schema_1.categories)
        .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.eq)(schema_1.categories.id, req.params.id), (0, drizzle_orm_1.eq)(schema_1.categories.userId, userId)));
    res.status(204).send();
}));
exports.default = router;
//# sourceMappingURL=categories.js.map