import { Request, Response } from "express";

import { ExternalInvoiceService } from "./externalInvoice.service";

const service = new ExternalInvoiceService();

export class ExternalInvoiceController {

    //Obtener todas las facturas
     
    async getAll(
        req: Request,
        res: Response
    ) {

        try {

            const invoices =
                await service.getAll();

            res.json(invoices);

        }
        catch (error) {

            res.status(500).json({
                message:
                    "Error al obtener facturas",
                error
            });

        }

    }

    //Obtener factura por ID
     
    async getById(
        req: Request,
        res: Response
    ) {

        try {

            const invoice =
                await service.getById(
                    req.params.id as string
                );

            if (!invoice) {

                return res.status(404).json({
                    message:
                        "Factura no encontrada"
                });

            }

            res.json(invoice);

        }
        catch (error) {

            res.status(500).json({
                message:
                    "Error al obtener factura",
                error
            });

        }

    }

    //Crear factura
     
    async create(
        req: Request,
        res: Response
    ) {

        try {

            const invoice =
                await service.create(
                    req.body
                );

            res.status(201).json(
                invoice
            );

        }
        catch (error) {

            res.status(500).json({
                message:
                    "Error al crear factura",
                error
            });

        }

    }

    //Actualizar factura
     
    async update(
        req: Request,
        res: Response
    ) {

        try {

            const invoice =
                await service.update(

                    req.params.id as string,
                    req.body

                );

            if (!invoice) {

                return res.status(404).json({
                    message:
                        "Factura no encontrada"
                });

            }

            res.json(invoice);

        }
        catch (error) {

            res.status(500).json({
                message:
                    "Error al actualizar factura",
                error
            });

        }

    }

    //Buscar por estado
    
    async getByStatus(
        req: Request,
        res: Response
    ) {

        try {

            const invoices =
                await service.getByStatus(
                    req.params.status as string
                );

            res.json(invoices);

        }
        catch (error) {

            res.status(500).json({
                message:
                    "Error al buscar facturas",
                error
            });

        }

    }

    //Buscar por zona
    
    async getByZone(
        req: Request,
        res: Response
    ) {

        try {

            const invoices =
                await service.getByZone(
                    req.params.zone as string
                );

            res.json(invoices);

        }
        catch (error) {

            res.status(500).json({
                message:
                    "Error al buscar facturas",
                error
            });

        }

    }

    //Eliminar factura
    
    async delete(
        req: Request,
        res: Response
    ) {

        try {

            const invoice =
                await service.delete(
                    req.params.id as string 
                );

            if (!invoice) {

                return res.status(404).json({
                    message:
                        "Factura no encontrada"
                });

            }

            res.json({
                message:
                    "Factura eliminada correctamente"
            });

        }
        catch (error) {

            res.status(500).json({
                message:
                    "Error al eliminar factura",
                error
            });

        }

    }

}