/**
 * Form factory functions for PixieSprout.
 * Creates fresh form-state objects for plant, space, setup, and event forms.
 */
import type { HumidityProfile, LightProfile, PlantSetupProfile, RoomProfile } from '../types/garden';
import type {
  PlantEventFormState,
  PlantFormState,
  PlantMemoryEventType,
  SetupFormState,
  SpaceFormState,
} from '../types/appTypes';
import { getPlantMemoryCategory } from './plant/plantMemoryHelpers';

export function makeEmptyPlantForm(): PlantFormState {
  return {
    name: '',
    scientificName: '',
    plantType: 'houseplant',
    growthStage: 'unsure',
    timeOwned: 'unsure',
    photoUri: undefined,
    spaceId: null,
    notes: '',
  };
}

export function makeEmptySpaceForm(): SpaceFormState {
  return {
    name: '',
    type: '' as SpaceFormState['type'],
    spaceType: '' as SpaceFormState['spaceType'],
    lightProfile: '' as SpaceFormState['lightProfile'],
    humidityProfile: '' as SpaceFormState['humidityProfile'],
    windowDirections: [],
    lightLevel: 'unsure',
    outdoorSunTimings: [],
    outdoorLightQuality: '' as SpaceFormState['outdoorLightQuality'],
    usesAmbientArtificialLight: null as any,
    ambientArtificialLightTypes: [],
    ambientArtificialLightHoursPerDay: undefined,
    humidityEstimate: 'unsure',
    temperatureEstimate: '' as SpaceFormState['temperatureEstimate'],
    notes: undefined,
    elevation: undefined,
    exposure: undefined,
    photoUri: undefined,
    environmentPhotoUri: undefined,
    environmentPhotos: [],
  };
}

export function lightLevelFromProfile(profile: LightProfile): RoomProfile['lightLevel'] {
  if (profile === 'low_light') return 'low';
  if (profile === 'medium_light') return 'medium';
  return profile;
}

export function humidityEstimateFromProfile(profile: HumidityProfile): RoomProfile['humidityEstimate'] {
  return profile === 'average' ? 'normal' : profile;
}

export function makeEmptySetupForm(): SetupFormState {
  return {
    roomId: undefined,
    distanceFromWindow: 'unsure',
    plantedIn: undefined,
    inGroundKind: undefined,
    hydroSetup: undefined,
    potType: undefined,
    potSize: undefined,
    drainage: undefined,
    mediumType: undefined,
    mediumTypes: [],
    customMedium: undefined,
    customMediumComponents: [],
    topDressing: [],
    customTopDressing: undefined,
    wateringMethod: undefined,
    wateringMethods: [],
    wateringStyle: undefined,
    wateringAutomated: undefined,
    customWatering: undefined,
    usesDedicatedArtificialLight: undefined,
    dedicatedArtificialLightTypes: [],
    dedicatedArtificialLightHoursPerDay: undefined,
    dedicatedArtificialLightDistance: undefined,
    fertilizer: undefined,
    notes: undefined,
    photoUris: [],
    setupPhotoUri: undefined,
    setupPhotos: [],
    sharedContainerId: undefined,
  };
}

export function makeEmptyPlantEventForm(type: PlantMemoryEventType = 'progress_photo'): PlantEventFormState {
  return {
    editingEventId: undefined,
    type,
    eventCategory: getPlantMemoryCategory(type),
    showCategoryPicker: true,
    title: '',
    note: '',
    photoUri: undefined,
    eventDate: new Date().toISOString(),
  };
}

export function isInGroundPotType(potType?: PlantSetupProfile['potType']): boolean {
  return potType === 'ground' || potType === 'raised_bed' || potType === 'garden_bed';
}
