import api from './api';

export interface ReservoirCheckData {
    tankName: string;
    tankType: 'VEG' | 'FLOWER';
    levelPercent: number;
    status: 'OK' | 'LOW' | 'CRITICAL';
    photoUrl?: string;
    notes?: string;
}

export interface IrrigationCheckData {
    zoneName: string;
    drippersTotal: number;
    drippersWorking: number;
    drippersFailed: string[];
    waterFlow: boolean;
    nutrientsMixed: boolean;
    scheduleActive: boolean;
    photoUrl?: string;
    issues?: string;
}

export interface PlantHealthCheckData {
    zoneName: string;
    healthStatus: 'GOOD' | 'FAIR' | 'NEEDS_ATTENTION';
    pestsObserved: boolean;
    pestType?: string;
    waterAccess: 'OK' | 'ISSUES';
    foodAccess: 'OK' | 'ISSUES';
    flaggedForAttention: boolean;
    issuePhotoUrl?: string;
    referencePhotoUrl?: string;
    notes?: string;
}

export interface Walkthrough {
    id: string;
    date: string;
    completedBy: string;
    startTime: string;
    endTime?: string;
    status: 'IN_PROGRESS' | 'COMPLETED' | 'INCOMPLETE';
    user: {
        id: string;
        name: string;
        email: string;
        role: string;
    };
    reservoirChecks: ReservoirCheckData[];
    irrigationChecks: IrrigationCheckData[];
    plantHealthChecks: PlantHealthCheckData[];
}

export const walkthroughApi = {
    /**
     * Start a new daily walkthrough
     */
    async create(): Promise<Walkthrough> {
        const { data } = await api.post('/walkthroughs');
        return data;
    },

    /**
     * Get all walkthroughs
     */
    async getAll(): Promise<Walkthrough[]> {
        const { data } = await api.get('/walkthroughs');
        return data;
    },

    /**
     * Get today's walkthrough (if exists)
     */
    async getToday(): Promise<Walkthrough | null> {
        try {
            const { data } = await api.get('/walkthroughs/today');
            return data;
        } catch (error: any) {
            if (error.response?.status === 404) {
                return null;
            }
            throw error;
        }
    },

    /**
     * Get a specific walkthrough
     */
    async getById(id: string): Promise<Walkthrough> {
        const { data } = await api.get(`/walkthroughs/${id}`);
        return data;
    },

    /**
     * Complete a walkthrough
     */
    async complete(id: string): Promise<Walkthrough> {
        const { data } = await api.post(`/walkthroughs/${id}/complete`);
        return data;
    },

    /**
     * Add a reservoir check
     */
    async addReservoirCheck(walkthroughId: string, check: ReservoirCheckData): Promise<void> {
        await api.post(`/walkthroughs/${walkthroughId}/reservoir-checks`, check);
    },

    /**
     * Add an irrigation check
     */
    async addIrrigationCheck(walkthroughId: string, check: IrrigationCheckData): Promise<void> {
        await api.post(`/walkthroughs/${walkthroughId}/irrigation-checks`, check);
    },

    /**
     * Add a plant health check
     */
    async addPlantHealthCheck(walkthroughId: string, check: PlantHealthCheckData): Promise<void> {
        await api.post(`/walkthroughs/${walkthroughId}/plant-health-checks`, check);
    },
};
