import { PaintingScheme } from '../lib/compat.js';

export const createPaintingScheme = async (data: any & { organizationId?: string }) => {
    return await PaintingScheme.create({ ...data, organization_id: data.organizationId });
};

export const getPaintingSchemesByProject = async (projectId: string, organizationId?: string) => {
    const filter: any = { project_id: projectId };
    if (organizationId) {
        filter.organization_id = organizationId;
    }
    return await PaintingScheme.find(filter);
};

export const getPaintingSchemeById = async (id: string) => {
    return await PaintingScheme.findById(id);
};

export const updatePaintingScheme = async (id: string, data: any, organizationId?: string) => {
    const existing = await PaintingScheme.findById(id);
    if (!existing) return null;
    
    if (organizationId && existing.organization_id && existing.organization_id !== organizationId) {
        return null;
    }
    
    return await PaintingScheme.findByIdAndUpdate(id, data);
};

export const deletePaintingScheme = async (id: string) => {
    return await PaintingScheme.findByIdAndDelete(id);
};

export const clonePaintingScheme = async (id: string, newData: any) => {
    const original = await PaintingScheme.findById(id);
    if (!original) return null;
    
    return await PaintingScheme.create({ ...original, ...newData, id: undefined });
};

console.log('✅ PaintingSchemeService loaded with compatibility');
