/**
 * Breadcrumb Service
 * Manages breadcrumb navigation across admin panel
 */

class BreadcrumbService {
  constructor() {
    this.breadcrumbs = [];
  }

  /**
   * Add a breadcrumb item
   * @param {string} title - Display text
   * @param {string} url - URL to navigate to (optional for last item)
   * @param {string} icon - Font-awesome icon class (optional)
   */
  add(title, url = null, icon = null) {
    this.breadcrumbs.push({
      title,
      url,
      icon
    });
    return this; // Allow chaining
  }

  /**
   * Reset breadcrumbs
   */
  reset() {
    this.breadcrumbs = [];
    return this;
  }

  /**
   * Get all breadcrumbs
   */
  get() {
    return this.breadcrumbs;
  }

  /**
   * Get breadcrumbs with home prefix
   */
  getWithHome(homeUrl = '/admin/dashboard') {
    return [
      {
        title: 'Dashboard',
        url: homeUrl,
        icon: 'fas fa-home'
      },
      ...this.breadcrumbs
    ];
  }

  /**
   * Generate HTML breadcrumb navigation
   */
  render(homeUrl = '/admin/dashboard') {
    const allCrumbs = this.getWithHome(homeUrl);

    let html = '<nav aria-label="breadcrumb" class="mb-4">\n';
    html += '  <ol class="breadcrumb">\n';

    allCrumbs.forEach((crumb, index) => {
      const isLast = index === allCrumbs.length - 1;

      if (isLast) {
        // Last item - no link
        html += `    <li class="breadcrumb-item active" aria-current="page">\n`;
        if (crumb.icon) {
          html += `      <i class="${crumb.icon}"></i> ${crumb.title}\n`;
        } else {
          html += `      ${crumb.title}\n`;
        }
        html += `    </li>\n`;
      } else {
        // Clickable item
        html += `    <li class="breadcrumb-item">\n`;
        html += `      <a href="${crumb.url}">\n`;
        if (crumb.icon) {
          html += `        <i class="${crumb.icon}"></i> ${crumb.title}\n`;
        } else {
          html += `        ${crumb.title}\n`;
        }
        html += `      </a>\n`;
        html += `    </li>\n`;
      }
    });

    html += '  </ol>\n';
    html += '</nav>\n';

    return html;
  }

  /**
   * Generate JSON breadcrumb data (for API responses)
   */
  toJSON(homeUrl = '/admin/dashboard') {
    return this.getWithHome(homeUrl);
  }

  /**
   * Get breadcrumb path as text
   * Example: "Dashboard > Users > Edit User"
   */
  toString(separator = ' > ') {
    return this.breadcrumbs.map(b => b.title).join(separator);
  }

  /**
   * Static method: Create service and add items in one call
   */
  static create(items = [], homeUrl = '/admin/dashboard') {
    const service = new BreadcrumbService();

    // Add dashboard
    service.add('Dashboard', homeUrl, 'fas fa-home');

    // Add provided items
    if (Array.isArray(items)) {
      items.forEach(item => {
        if (typeof item === 'string') {
          service.add(item);
        } else if (typeof item === 'object') {
          service.add(item.title, item.url, item.icon);
        }
      });
    }

    return service;
  }
}

module.exports = BreadcrumbService;
