import { isSameDay, parseISO } from 'date-fns';

/**
 * Determines the visibility and display properties of a calendar event based on the current user and association.
 * 
 * Logic:
 * - Admins/Employees: Always see full details.
 * - Clients: 
 *    - Own Association (primary_client_id matches): Full details.
 *    - Shared/Public:
 *         - if is_blocked == true: Red 'Unavailable' block.
 *         - if is_blocked == false: Full details.
 * 
 * @param {Object} event - The calendar event object.
 * @param {Object} userRole - The user role object (containing .role and .client_id).
 * @returns {Object} { isOwnEvent, isBlockedDate, canViewDetails, displayClass, isHidden }
 */
export const getEventVisibility = (event, userRole) => {
  if (!event) return { isHidden: true, isOwnEvent: false, isBlockedDate: false, canViewDetails: false, displayClass: '' };

  const isAdmin = userRole?.role === 'admin' || userRole?.role === 'employee';
  const currentClientId = userRole?.client_id;

  // --- ADMIN VIEW ---
  if (isAdmin) {
    if (event.is_blocked || event.type === 'blocked') {
      return {
        isOwnEvent: false,
        isBlockedDate: true,
        canViewDetails: false,
        displayClass: 'bg-red-600 text-white border-red-700 px-1 py-0.5 rounded opacity-90 cursor-not-allowed',
        isHidden: false
      };
    }
    return { 
        isOwnEvent: true, 
        isBlockedDate: false, 
        canViewDetails: true,
        displayClass: getEventColor(event.type),
        isHidden: false
    };
  }

  // --- CLIENT VIEW ---
  
  // 1. Identify ownership
  const isMine = event.is_mine || (event.primary_client_id && event.primary_client_id === currentClientId);
  
  if (isMine) {
      // If it's MY event, I see full details regardless of blocked status (usually)
      // But if my admin marked it as 'blocked' for ME, maybe I should see it as blocked?
      // Assuming 'is_blocked' primarily affects OTHER viewers. 
      // Owners should see their own events fully.
      return {
          isOwnEvent: true,
          isBlockedDate: false,
          canViewDetails: true,
          displayClass: getEventColor(event.type),
          isHidden: false
      };
  }

  // 2. Shared or Public Events
  // The event is visible (because RPC returned it), so now check if it's blocked for me.
  
  if (event.is_blocked || event.type === 'blocked') {
      return {
          isOwnEvent: false,
          isBlockedDate: true,
          canViewDetails: false, // Cannot see details
          displayClass: 'bg-red-500 text-transparent border-red-600 cursor-not-allowed h-1.5 overflow-hidden my-0.5 rounded-sm hover:bg-red-600 opacity-60',
          isHidden: false
      };
  }
  
  // If not blocked, and I can see it (shared/public), show normally
  return {
      isOwnEvent: false,
      isBlockedDate: false,
      canViewDetails: true,
      displayClass: getEventColor('public'), // Or event.type if we want to show true type
      isHidden: false
  };
};

/**
 * Helper to get standard colors for event types
 */
export const getEventColor = (type) => {
  switch (type?.toLowerCase()) {
    case 'meeting': return 'bg-blue-100 text-blue-800 border-blue-200 hover:bg-blue-200';
    case 'maintenance': return 'bg-orange-100 text-orange-800 border-orange-200 hover:bg-orange-200';
    case 'community': return 'bg-teal-100 text-teal-800 border-teal-200 hover:bg-teal-200';
    case 'public': return 'bg-green-100 text-green-800 border-green-200 hover:bg-green-200';
    case 'violation': return 'bg-red-100 text-red-800 border-red-200 hover:bg-red-200';
    case 'blocked': return 'bg-red-500 text-white border-red-600 hover:bg-red-600';
    default: return 'bg-slate-100 text-slate-800 border-slate-200 hover:bg-slate-200';
  }
};