import { Alert, Text } from 'react-native';

import { FormSection } from '~/components/ui/form-section';
import { SheetSelect } from '~/components/ui/sheet-select';
import { SwitchRow } from '~/components/ui/switch-row';

type Cadence = 'daily' | 'weekly' | 'manual';
type MaintenanceMode = 'watch' | 'auto_pr' | 'paused';

export const SpoonMaintenanceSettingsForm = ({
  maintenance,
  onUpdateMaintenance,
  onUpdateSpoon,
  saving,
  spoon,
}: {
  maintenance?: {
    autoRefreshEnabled: boolean;
    autoReviewEnabled: boolean;
    autoSyncEnabled: boolean;
  };
  onUpdateMaintenance: (patch: {
    autoRefreshEnabled?: boolean;
    autoReviewEnabled?: boolean;
    autoSyncEnabled?: boolean;
  }) => Promise<void>;
  onUpdateSpoon: (patch: {
    maintenanceMode?: MaintenanceMode;
    syncCadence?: Cadence;
  }) => Promise<void>;
  saving: boolean;
  spoon: { maintenanceMode: MaintenanceMode; syncCadence: Cadence };
}) => {
  const updateMaintenance = (
    patch: Parameters<typeof onUpdateMaintenance>[0],
  ) =>
    void onUpdateMaintenance(patch).catch((error: unknown) => {
      console.error(error);
      Alert.alert('Could not save maintenance settings.');
    });

  const updateSpoon = (patch: Parameters<typeof onUpdateSpoon>[0]) =>
    void onUpdateSpoon(patch).catch((error: unknown) => {
      console.error(error);
      Alert.alert('Could not save Spoon settings.');
    });

  return (
    <>
      <FormSection
        description='These settings control scheduled checks and safe automation.'
        title='Maintenance settings'
      >
        <SwitchRow
          description='Let scheduled checks consider this Spoon.'
          label='Auto refresh'
          value={maintenance?.autoRefreshEnabled ?? true}
          onValueChange={(autoRefreshEnabled) =>
            updateMaintenance({ autoRefreshEnabled })
          }
        />
        <SwitchRow
          label='Auto review'
          value={maintenance?.autoReviewEnabled ?? true}
          onValueChange={(autoReviewEnabled) =>
            updateMaintenance({ autoReviewEnabled })
          }
        />
        <SwitchRow
          label='Auto sync'
          value={maintenance?.autoSyncEnabled ?? false}
          onValueChange={(autoSyncEnabled) =>
            updateMaintenance({ autoSyncEnabled })
          }
        />
        {saving ? (
          <Text className='text-muted-foreground text-xs'>Saving...</Text>
        ) : null}
      </FormSection>
      <FormSection title='Spoon settings'>
        <SheetSelect
          label='Cadence'
          options={[
            { label: 'Daily', value: 'daily' },
            { label: 'Weekly', value: 'weekly' },
            { label: 'Manual', value: 'manual' },
          ]}
          value={spoon.syncCadence}
          onChange={(syncCadence) => updateSpoon({ syncCadence })}
        />
        <SheetSelect
          label='Maintenance mode'
          options={[
            { label: 'Watch', value: 'watch' },
            { label: 'Auto PR', value: 'auto_pr' },
            { label: 'Paused', value: 'paused' },
          ]}
          value={spoon.maintenanceMode}
          onChange={(maintenanceMode) => updateSpoon({ maintenanceMode })}
        />
      </FormSection>
    </>
  );
};
