import { serverFetchJson } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { cache } from "react";
import "server-only";

export type ScheduleType = "all" | "rough" | "detailed" | "manual";

// Rough
export interface RoughProdScheduleResult {
    id: number;
    scheduleAt: number[];
    produceAt: number[];
    schedulePeriod: number[];
    schedulePeriodTo: number[];
    totalEstProdCount: number;
    totalFGType: number;
    type: string;
    prodScheduleLinesByFg: RoughProdScheduleLineResultByFg[];
    prodScheduleLinesByFgByDate: {
        [assignDate: number]: RoughProdScheduleLineResultByFg[];
    };
    prodScheduleLinesByBom: RoughProdScheduleLineResultByBom[];
    prodScheduleLinesByBomByDate: {
        [assignDate: number]: RoughProdScheduleLineResultByBomByDate[];
    };
}

export interface RoughProdScheduleLineResultByFg {
    id: number;
    code: string;
    name: string;
    type: string;
    availableQty: number;
    prodQty: number;
    uomName: string;
    lastMonthAvgSales: number;
    estCloseBal: number;
    priority: number;
    assignDate: number;
    bomMaterials: RoughProdScheduleLineBomMaterialResult[];
}

export interface RoughProdScheduleLineBomMaterialResult {
    id: number;
    code: string;
    name: string;
    type: string;
    availableQty: number;
    demandQty: number;
    uomName: string;
}

export interface RoughProdScheduleLineResultByBom {
    id: number;
    code: string;
    name: string;
    type: string;
    availableQty: number;
    totalDemandQty: number;
    demandQty1: number;
    demandQty2: number;
    demandQty3: number;
    demandQty4: number;
    demandQty5: number;
    demandQty6: number;
    demandQty7: number;
    uomName: string;
}

export interface RoughProdScheduleLineResultByBomByDate {
    id: number;
    code: string;
    name: string;
    type: string;
    availableQty: number;
    demandQty: number;
    assignDate: number;
    uomName: string;
}

// Detailed
export interface DetailedProdScheduleResult {
    id: number;
    produceAt: number[];
    scheduleAt: number[];
    totalEstProdCount: number;
    totalFGType: number;
    prodScheduleLines: DetailedProdScheduleLineResult[];
}

export interface DetailedProdScheduleLineResult {
    id: number;
    bomMaterials: DetailedProdScheduleLineBomMaterialResult[];
    jobNo: string;
    code: string;
    name: string;
    type: string;
    demandQty: number;
    bomOutputQty: number;
    uomName: string;
    prodTimeInMinute: DetailedProdScheduleLineProdTimeResult[];
    priority: number;
    approved: boolean;
    proportion: number;
    lastMonthAvgSales: number; 
    avgQtyLastMonth: number;   // Average usage last month
    stockQty: number;         // Warehouse stock quantity
    daysLeft: number;          // Days remaining before stockout
    needNoOfJobOrder: number;
    prodQty: number;
    outputQty: number;
}

export interface DetailedProdScheduleLineBomMaterialResult {
    id: number;
    code: string;
    name: string;
    type: string;
    availableQty: number;
    demandQty: number;
    uomName: string;
}

export interface DetailedProdScheduleLineProdTimeResult {
    equipName: string;
    totalMinutes: number;
}

// API
export const fetchRoughProdScheduleDetail = cache(async (id: number) => {
    return serverFetchJson<RoughProdScheduleResult>(`${BASE_API_URL}/productionSchedule/detail/rough/${id}`, {
        method: "GET",
        headers: { "Content-Type": "application/json" },
        next: {
            tags: ["prodSchedule"]
        }
    })
})

export const fetchDetailedProdScheduleDetail = cache(async (id: number) => {
    return serverFetchJson<DetailedProdScheduleResult>(`${BASE_API_URL}/productionSchedule/detail/detailed/${id}`, {
        method: "GET",
        headers: { "Content-Type": "application/json" },
        next: {
            tags: ["prodSchedule"]
        }
    })
})