import {
  getCascadeSummary,
  CatalogSelectionContext,
} from '../catalog/catalogShared';
import { buildDefaultValues } from '../catalog/catalogSchemas';
import {
  CatalogEngine,
  CatalogMake,
  CatalogModel,
  CatalogTrim,
  CatalogYear,
} from '../types/admin.types';

const baseMake: CatalogMake = {
  id: 'make-1',
  name: 'Honda',
  createdAt: '2024-01-01T00:00:00Z',
  updatedAt: '2024-01-01T00:00:00Z',
};

const baseModel: CatalogModel = {
  id: 'model-1',
  makeId: baseMake.id,
  name: 'Civic',
  createdAt: '2024-01-02T00:00:00Z',
  updatedAt: '2024-01-02T00:00:00Z',
};

const baseYear: CatalogYear = {
  id: 'year-1',
  modelId: baseModel.id,
  year: 2024,
  createdAt: '2024-01-03T00:00:00Z',
  updatedAt: '2024-01-03T00:00:00Z',
};

const baseTrim: CatalogTrim = {
  id: 'trim-1',
  yearId: baseYear.id,
  name: 'Sport',
  createdAt: '2024-01-04T00:00:00Z',
  updatedAt: '2024-01-04T00:00:00Z',
};

const baseEngine: CatalogEngine = {
  id: 'engine-1',
  trimId: baseTrim.id,
  name: '2.0T',
  displacement: '2.0L',
  cylinders: 4,
  fuelType: 'Gasoline',
  createdAt: '2024-01-05T00:00:00Z',
  updatedAt: '2024-01-05T00:00:00Z',
};

describe('getCascadeSummary', () => {
  it('describes dependent counts for makes', () => {
    const modelsByMake = new Map<string, CatalogModel[]>([
      [baseMake.id, [baseModel]],
    ]);
    const yearsByModel = new Map<string, CatalogYear[]>([
      [baseModel.id, [baseYear]],
    ]);
    const trimsByYear = new Map<string, CatalogTrim[]>([
      [baseYear.id, [baseTrim]],
    ]);
    const enginesByTrim = new Map<string, CatalogEngine[]>([
      [baseTrim.id, [baseEngine]],
    ]);

    const summary = getCascadeSummary(
      'makes',
      [baseMake],
      modelsByMake,
      yearsByModel,
      trimsByYear,
      enginesByTrim
    );

    expect(summary).toContain('1 model');
    expect(summary).toContain('1 year');
    expect(summary).toContain('1 trim');
    expect(summary).toContain('1 engine');
  });

  it('returns empty string when nothing selected', () => {
    const summary = getCascadeSummary(
      'models',
      [],
      new Map(),
      new Map(),
      new Map(),
      new Map()
    );
    expect(summary).toBe('');
  });
});

describe('buildDefaultValues', () => {
  it('prefills parent context for create operations', () => {
    const context: CatalogSelectionContext = {
      level: 'models',
      make: baseMake,
    };

    const defaults = buildDefaultValues('models', 'create', undefined, context);

    expect(defaults.makeId).toBe(baseMake.id);
    expect(defaults.name).toBe('');
  });

  it('hydrates existing entity data for editing engines', () => {
    const context: CatalogSelectionContext = {
      level: 'engines',
      make: baseMake,
      model: baseModel,
      year: baseYear,
      trim: baseTrim,
    };

    const defaults = buildDefaultValues(
      'engines',
      'edit',
      baseEngine,
      context
    );

    expect(defaults.name).toBe(baseEngine.name);
    expect(defaults.trimId).toBe(baseTrim.id);
    expect(defaults.displacement).toBe('2.0L');
    expect(defaults.cylinders).toBe(4);
    expect(defaults.fuelType).toBe('Gasoline');
  });
});
