import { Request, Response } from 'express';
import * as paintingSchemeService from '../services/paintingSchemeService.js';

export const createPaintingScheme = async (req: Request, res: Response) => {
    try {
        const organizationId = req.appUser?.organizationId;
        console.log("Creating scheme with payload:", req.body);
        const scheme = await paintingSchemeService.createPaintingScheme({ ...req.body, organizationId });
        console.log("Created scheme result:", scheme);
        res.status(201).json(scheme);
    } catch (error: unknown) {
        const message = error instanceof Error ? error.message : 'Unknown error';
        res.status(500).json({ error: message });
    }
};

export const getPaintingSchemesByProject = async (req: Request, res: Response) => {
    try {
        const { projectId } = req.params;
        const organizationId = req.appUser?.organizationId;
        const schemes = await paintingSchemeService.getPaintingSchemesByProject(projectId as string, organizationId);
        res.json(schemes);
    } catch (error: unknown) {
        const message = error instanceof Error ? error.message : 'Unknown error';
        res.status(500).json({ error: message });
    }
};


export const updatePaintingScheme = async (req: Request, res: Response) => {
    try {
        const organizationId = req.appUser?.organizationId;
        console.log("---------------------------------------------------");
        console.log(`UPDATE REQUEST: ID=${req.params.id}`);
        console.log(`User Org ID: ${organizationId}`);
        console.log(`Payload keys: ${Object.keys(req.body)}`);

        const scheme = await paintingSchemeService.updatePaintingScheme(req.params.id as string, req.body, organizationId);

        console.log(`UPDATE RESULT: ${scheme ? 'SUCCESS' : 'NULL (Doc not found or not matched)'}`);
        if (scheme) {
            console.log(`Updated Doc Coat: ${scheme.coat}`);
        }
        console.log("---------------------------------------------------");

        res.json(scheme);
    } catch (error: unknown) {
        const message = error instanceof Error ? error.message : 'Unknown error';
        res.status(500).json({ error: message });
    }
};

export const deletePaintingScheme = async (req: Request, res: Response) => {
    try {
        const organizationId = req.appUser?.organizationId;
        await paintingSchemeService.deletePaintingScheme(req.params.id as string, organizationId);
        res.status(204).send();
    } catch (error: unknown) {
        const message = error instanceof Error ? error.message : 'Unknown error';
        res.status(500).json({ error: message });
    }
};

export const getAllPaintingSchemes = async (req: Request, res: Response) => {
    try {
        const organizationId = req.appUser?.organizationId;
        const schemes = await paintingSchemeService.getAllSchemes(organizationId);
        res.json(schemes);
    } catch (error: unknown) {
        const message = error instanceof Error ? error.message : 'Unknown error';
        res.status(500).json({ error: message });
    }
};
