'use server';

import Farms, { Farm } from '@/models/farm';
import Processors, { Processor } from '@/models/processor';

import db from '../db/connection';

export async function getFarms() {
    await db();
    const farms = await Farms.find();
    return JSON.parse(JSON.stringify(farms));
}

export async function getProcessors() {
    await db();
    const processor = await Processors.find();
    return JSON.parse(JSON.stringify(processor));
}

export async function getSupplyChainData() {
    await db();
    const [farms, processors] = await Promise.all([getFarms(), getProcessors()]);
    return { farms, processors };
}

export async function addFarm(farm: Farm) {
    await db();
    const newFarm = new Farms(farm);
    await newFarm.save();
}

export async function addProcessor(processor: Processor) {
    await db();
    const newProcessor = new Processors(processor);
    await newProcessor.save();
}
