import api from './api';

export interface Visitor {
    id: string;
    name: string;
    company?: string;
    email?: string;
    phone?: string;
    photoUrl?: string;
    idType?: string;
    type: 'VISITOR' | 'CONTRACTOR' | 'INSPECTOR' | 'VENDOR' | 'DELIVERY' | 'OTHER';
    purpose: string;
    ndaAccepted: boolean;
    ndaAcceptedAt?: string;
    createdAt: string;
}

export interface VisitorLog {
    id: string;
    visitorId: string;
    status: 'PRE_REGISTERED' | 'CHECKED_IN' | 'CHECKED_OUT' | 'DENIED' | 'REVOKED';
    entryTime: string;
    exitTime?: string;
    escortId?: string;
    escort?: { id: string; name: string };
    badgeNumber?: string;
    badgeExpiry?: string;
    zones: string[];
    visitor?: Visitor;
}

export interface AccessZone {
    id: string;
    name: string;
    code: string;
    description?: string;
    escortRequired: boolean;
    badgeRequired: boolean;
    ndaRequired: boolean;
    allowedTypes: string[];
    maxOccupancy?: number;
}

export interface ActiveVisitor {
    logId: string;
    visitorId: string;
    name: string;
    company?: string;
    type: string;
    purpose: string;
    entryTime: string;
    escort?: { id: string; name: string };
    badgeNumber?: string;
    badgeExpiry?: string;
    zones: string[];
}

export const visitorsApi = {
    // Visitors
    async getAll(params?: { status?: string; type?: string; search?: string; page?: number }): Promise<{
        visitors: (Visitor & { logs: VisitorLog[] })[];
        pagination: { page: number; limit: number; total: number; pages: number };
    }> {
        const response = await api.get('/visitors', { params });
        return response.data;
    },

    async getActive(): Promise<{ count: number; visitors: ActiveVisitor[] }> {
        const response = await api.get('/visitors/active');
        return response.data;
    },

    async getById(id: string): Promise<Visitor & { logs: VisitorLog[] }> {
        const response = await api.get(`/visitors/${id}`);
        return response.data;
    },

    async create(data: Partial<Visitor>): Promise<Visitor> {
        const response = await api.post('/visitors', data);
        return response.data;
    },

    async checkIn(id: string, data: {
        escortId?: string;
        zones?: string[];
        temperatureF?: number;
        signature?: string;
        ndaAccepted?: boolean;
        notes?: string;
    }): Promise<{ success: boolean; badgeNumber: string; visitId: string; log: VisitorLog }> {
        const response = await api.post(`/visitors/${id}/check-in`, data);
        return response.data;
    },

    async checkOut(id: string, notes?: string): Promise<{ success: boolean; duration: number; log: VisitorLog }> {
        const response = await api.post(`/visitors/${id}/check-out`, { notes });
        return response.data;
    },

    async revoke(id: string, notes: string): Promise<{ success: boolean; log: VisitorLog }> {
        const response = await api.post(`/visitors/${id}/revoke`, { notes });
        return response.data;
    },

    async getReport(params: { startDate?: string; endDate?: string; type?: string }): Promise<any> {
        const response = await api.get('/visitors/report', { params });
        return response.data;
    },

    // Access Zones
    async getZones(): Promise<AccessZone[]> {
        const response = await api.get('/zones');
        return response.data;
    },

    async createZone(data: Partial<AccessZone>): Promise<AccessZone> {
        const response = await api.post('/zones', data);
        return response.data;
    },

    async getZoneOccupancy(id: string): Promise<{
        zone: { id: string; name: string; code: string };
        maxOccupancy?: number;
        currentOccupancy: number;
        atCapacity: boolean;
        visitors: any[];
    }> {
        const response = await api.get(`/zones/${id}/occupancy`);
        return response.data;
    },

    async logZoneEntry(zoneId: string, visitorLogId: string): Promise<{ success: boolean }> {
        const response = await api.post(`/zones/${zoneId}/log-entry`, { visitorLogId });
        return response.data;
    }
};
