import { describe, expect, test } from 'vitest';
import { buildAppHash, parseAppHash } from './appRoute';

describe('parseAppHash', () => {
  test('parses detail page keyword from hash query', () => {
    expect(parseAppHash('#/detail?keyword=%E7%B2%A4AG18312&protocol=JT808')).toEqual({
      page: 'detail',
      keyword: '粤AG18312',
      protocol: 'JT808',
      filters: {}
    });
  });

  test('parses history tab from hash query', () => {
    expect(parseAppHash('#/history?keyword=VIN001&protocol=JT808&tab=raw')).toEqual({
      page: 'history',
      keyword: 'VIN001',
      protocol: 'JT808',
      filters: {
        tab: 'raw'
      }
    });
  });

  test('parses history query page', () => {
    expect(parseAppHash('#/history-query?keyword=VIN001&protocol=GB32960&tab=raw')).toEqual({
      page: 'history-query',
      keyword: 'VIN001',
      protocol: 'GB32960',
      filters: {
        tab: 'raw'
      }
    });
  });

  test('parses vehicle list filters from hash query', () => {
    expect(parseAppHash('#/vehicles?coverage=multi&serviceStatus=degraded&online=online&bindingStatus=bound&archiveStatus=incomplete&archiveMissing=phone&missingProtocol=YUTONG_MQTT')).toEqual({
      page: 'vehicles',
      filters: {
        coverage: 'multi',
        serviceStatus: 'degraded',
        online: 'online',
        bindingStatus: 'bound',
        archiveStatus: 'incomplete',
        archiveMissing: 'phone',
        missingProtocol: 'YUTONG_MQTT'
      }
    });
  });

  test('parses alert event filters from hash query', () => {
    expect(parseAppHash('#/alert-events?keyword=%E7%B2%A4A&protocol=VEHICLE_SERVICE&issueType=NO_SOURCE')).toEqual({
      page: 'alert-events',
      keyword: '粤A',
      protocol: 'VEHICLE_SERVICE',
      filters: {
        issueType: 'NO_SOURCE'
      }
    });
  });

  test('keeps legacy quality governance hash compatible', () => {
    expect(parseAppHash('#/quality?keyword=%E7%B2%A4A&protocol=VEHICLE_SERVICE&issueType=NO_SOURCE')).toEqual({
      page: 'quality',
      keyword: '粤A',
      protocol: 'VEHICLE_SERVICE',
      filters: {
        issueType: 'NO_SOURCE'
      }
    });
  });

  test('parses notification rules page', () => {
    expect(parseAppHash('#/notification-rules')).toEqual({
      page: 'notification-rules',
      filters: {}
    });
  });

  test('parses map situation page', () => {
    expect(parseAppHash('#/map?keyword=VIN001&protocol=GB32960&online=online')).toEqual({
      page: 'map',
      keyword: 'VIN001',
      protocol: 'GB32960',
      filters: {
        online: 'online'
      }
    });
  });

  test('parses ops quality page', () => {
    expect(parseAppHash('#/ops-quality')).toEqual({
      page: 'ops-quality',
      filters: {}
    });
  });

  test('ignores unknown pages', () => {
    expect(parseAppHash('#/unknown?keyword=VIN001')).toEqual({});
  });
});

describe('buildAppHash', () => {
  test('builds shareable vehicle page hash with encoded keyword', () => {
    expect(buildAppHash({ page: 'history', keyword: '粤AG18312', protocol: 'GB32960' })).toBe('#/history?keyword=%E7%B2%A4AG18312&protocol=GB32960');
  });

  test('builds shareable raw history hash', () => {
    expect(buildAppHash({ page: 'history', keyword: 'VIN001', protocol: 'JT808', filters: { tab: 'raw' } })).toBe('#/history?keyword=VIN001&protocol=JT808&tab=raw');
  });

  test('builds shareable history query hash', () => {
    expect(buildAppHash({ page: 'history-query', keyword: 'VIN001', protocol: 'GB32960', filters: { tab: 'raw' } })).toBe('#/history-query?keyword=VIN001&protocol=GB32960&tab=raw');
  });

  test('builds shareable parsed fields history query hash', () => {
    expect(buildAppHash({ page: 'history-query', keyword: 'VIN001', protocol: 'GB32960', filters: { tab: 'fields', fields: 'gb32960.vehicle.speed_kmh' } })).toBe('#/history-query?keyword=VIN001&protocol=GB32960&tab=fields&fields=gb32960.vehicle.speed_kmh');
  });

  test('builds shareable vehicle list hash with filters', () => {
    expect(buildAppHash({ page: 'vehicles', filters: { coverage: 'multi', serviceStatus: 'degraded', archiveStatus: 'incomplete', archiveMissing: 'phone', missingProtocol: 'YUTONG_MQTT' } })).toBe('#/vehicles?coverage=multi&serviceStatus=degraded&archiveStatus=incomplete&archiveMissing=phone&missingProtocol=YUTONG_MQTT');
  });

  test('builds shareable alert events hash with filters', () => {
    expect(buildAppHash({ page: 'alert-events', keyword: '粤A', protocol: 'VEHICLE_SERVICE', filters: { issueType: 'NO_SOURCE' } })).toBe('#/alert-events?keyword=%E7%B2%A4A&protocol=VEHICLE_SERVICE&issueType=NO_SOURCE');
  });

  test('keeps building legacy quality hash when requested', () => {
    expect(buildAppHash({ page: 'quality', keyword: '粤A', protocol: 'VEHICLE_SERVICE', filters: { issueType: 'NO_SOURCE' } })).toBe('#/quality?keyword=%E7%B2%A4A&protocol=VEHICLE_SERVICE&issueType=NO_SOURCE');
  });

  test('builds notification rules page hash', () => {
    expect(buildAppHash({ page: 'notification-rules' })).toBe('#/notification-rules');
  });

  test('builds map situation page hash', () => {
    expect(buildAppHash({ page: 'map', keyword: 'VIN001', protocol: 'GB32960', filters: { online: 'online' } })).toBe('#/map?keyword=VIN001&protocol=GB32960&online=online');
  });

  test('builds ops quality page hash', () => {
    expect(buildAppHash({ page: 'ops-quality' })).toBe('#/ops-quality');
  });

  test('builds page-only hash when keyword is empty', () => {
    expect(buildAppHash({ page: 'alert-events', keyword: '' })).toBe('#/alert-events');
  });
});
