import { FastifyRequest, FastifyReply } from 'fastify';
import { CanvasApiEndpoint } from '../../entities/CanvasApiEndpoint';

export class CanvasApiEndpointController {
    async getAll(request: FastifyRequest, reply: FastifyReply) {
        const endpoints = await request.em.find(CanvasApiEndpoint, {});
        return reply.send(endpoints);
    }

    async create(request: FastifyRequest, reply: FastifyReply) {
        const { name, method, path, description, user } = request.body as any;
        if (!name || !method || !path) {
            return reply.status(400).send({ error: 'name, method, and path are required' });
        }
        const endpoint = new CanvasApiEndpoint();
        endpoint.name = name;
        endpoint.method = method;
        endpoint.path = path;
        endpoint.description = description;
        endpoint.user = user;
        await request.em.persistAndFlush(endpoint);
        return reply.send(endpoint);
    }

    async delete(request: FastifyRequest, reply: FastifyReply) {
        const id = Number(request.params['id']);
        if (!id) return reply.status(400).send({ error: 'Missing id' });
        const endpoint = await request.em.findOne(CanvasApiEndpoint, { id });
        if (!endpoint) return reply.status(404).send({ error: 'Not found' });
        await request.em.removeAndFlush(endpoint);
        return reply.send({ success: true });
    }
} 