/**
 * url-builder.js
 * Utility for building URLs based on admin settings and project context
 * Handles dynamic Storybook URL generation with hostname, port, skin
 */

import { useAdminStore } from '../stores/admin-store.js';
import { useProjectStore } from '../stores/project-store.js';

class URLBuilder {
  /**
   * Get Storybook URL for current context
   * @param {Object} options - Options for URL generation
   * @param {string} options.skin - Skin/theme name (optional, uses project default)
   * @param {string} options.projectId - Project ID (optional, uses current project)
   * @returns {string} Full Storybook URL
   */
  static getStorybookUrl(options = {}) {
    const adminStore = useAdminStore();
    const projectStore = useProjectStore();

    const skin = options.skin || projectStore.getCurrentProject()?.skinSelected || 'default';
    const projectId = options.projectId || projectStore.currentProjectId;

    const protocol = adminStore.state.isRemote ? 'https' : 'http';
    const hostname = adminStore.state.hostname;
    const port = adminStore.state.port;

    const baseUrl = `${protocol}://${hostname}:${port}/storybook/`;
    const params = new URLSearchParams();

    if (skin) params.append('skin', skin);
    if (projectId) params.append('project', projectId);

    const queryString = params.toString();
    return queryString ? `${baseUrl}?${queryString}` : baseUrl;
  }

  /**
   * Get API base URL for backend
   * @returns {string} API base URL
   */
  static getAPIBaseUrl() {
    const adminStore = useAdminStore();
    const protocol = adminStore.state.isRemote ? 'https' : 'http';
    const hostname = adminStore.state.hostname;

    return `${protocol}://${hostname}:3456/api`;
  }

  /**
   * Get Jira API URL (depends on backend for authentication)
   * @returns {string} Jira API endpoint URL
   */
  static getJiraApiUrl() {
    const baseUrl = this.getAPIBaseUrl();
    return `${baseUrl}/integrations/jira`;
  }

  /**
   * Get Figma API URL (depends on backend for authentication)
   * @returns {string} Figma API endpoint URL
   */
  static getFigmaApiUrl() {
    const baseUrl = this.getAPIBaseUrl();
    return `${baseUrl}/integrations/figma`;
  }

  /**
   * Get MCP bridge URL for tool execution
   * @returns {string} MCP bridge endpoint URL
   */
  static getMCPBridgeUrl() {
    const baseUrl = this.getAPIBaseUrl();
    return `${baseUrl}/mcp-bridge`;
  }

  /**
   * Build a link to a component in Storybook
   * @param {Object} component - Component info
   * @param {string} component.id - Component ID
   * @param {string} component.name - Component name
   * @returns {string} Link to Storybook component
   */
  static getComponentUrl(component) {
    const baseUrl = this.getStorybookUrl();
    const componentPath = `${component.id}--${component.name}`.toLowerCase().replace(/\s+/g, '-');
    return `${baseUrl}?path=/story/${componentPath}`;
  }

  /**
   * Build a link to theme/skin preview
   * @param {string} skin - Skin name
   * @returns {string} Link to Storybook with selected skin
   */
  static getSkinPreviewUrl(skin) {
    return this.getStorybookUrl({ skin });
  }
}

export default URLBuilder;
