'use client';

import { useEffect } from 'react';
import type { GenerationSettings } from '../controls/interaction-input/types';
import { DEFAULT_SETTINGS } from '../controls/interaction-input/constants';
import type { ModelConfig } from '@/types/model';
import type { GenerationType } from '../controls/interaction-input/types';

type SetSettings = (updater: (prev: GenerationSettings) => GenerationSettings) => void;

/**
 * Applies default model and resolution when the generation tab changes (image/video/audio/music).
 * No-op for text tab or when in edit mode.
 */
export function useInteractionDefaults(
  activeTab: GenerationType,
  availableModels: Record<string, ModelConfig>,
  lyricsModels: Record<string, ModelConfig>,
  musicModels: Record<string, ModelConfig>,
  setSettings: SetSettings,
  isEditMode: boolean
): void {
  useEffect(() => {
    if (activeTab === 'text' || isEditMode) return;

    const defaultModel =
      (activeTab === 'music'
        ? Object.values(musicModels).find((m) => m.is_default)?.id || Object.keys(musicModels)[0]
        : undefined) ||
      Object.values(availableModels).find((m) => m.is_default)?.id ||
      Object.keys(availableModels)[0] ||
      '';

    const defaultLyricsModel =
      Object.values(lyricsModels).find((m) => m.is_default)?.id ||
      Object.keys(lyricsModels)[0] ||
      '';

    const defaultMusicModel =
      Object.values(musicModels).find((m) => m.is_default)?.id ||
      Object.keys(musicModels)[0] ||
      '';

    let defaultResolution = DEFAULT_SETTINGS.resolution;

    if (activeTab === 'video' && defaultModel) {
      const modelConfig = availableModels[defaultModel];
      if (modelConfig?.resolutions) {
        const resolutions = Object.keys(modelConfig.resolutions);
        const match = resolutions.find((r) => r.toLowerCase() === '720p');
        defaultResolution = match || resolutions[0] || '720P';
      } else {
        defaultResolution = '720P';
      }
    } else if (activeTab === 'image' && defaultModel) {
        const modelConfig = availableModels[defaultModel];
        if (modelConfig?.resolutions) {
            const resolutions = Object.keys(modelConfig.resolutions);
            const match = resolutions.find((r) => r.toLowerCase() === '1k');
            defaultResolution = match || resolutions[0] || '1K';
        } else {
            defaultResolution = '1K';
        }
    } else if (activeTab === 'music') {
      // Music has no resolution concept; keep existing resolution untouched.
      defaultResolution = DEFAULT_SETTINGS.resolution;
    }

    setSettings((prev) => {
      if (activeTab === 'music') {
        const nextLyricsModel = prev.lyricsModel || defaultLyricsModel;
        const nextMusicModel = prev.musicModel || defaultMusicModel;
        const nextGenerateLyrics = prev.musicGenerateLyrics ?? false;
        const nextGenerateMusic = prev.musicGenerateMusic ?? true;
        const nextModel = nextGenerateMusic
          ? (nextMusicModel || defaultModel)
          : (nextLyricsModel || defaultLyricsModel || defaultModel);

        return {
          ...prev,
          resolution: defaultResolution,
          lyricsModel: nextLyricsModel,
          musicModel: nextMusicModel,
          musicTarget: prev.musicTarget || 'music',
          musicGenerateLyrics: nextGenerateLyrics,
          musicGenerateMusic: nextGenerateMusic,
          model: nextModel,
        };
      }

      return {
        ...prev,
        model: defaultModel,
        resolution: defaultResolution,
      };
    });
  }, [activeTab, availableModels, lyricsModels, musicModels, setSettings, isEditMode]);
}
