import { describe, expect, it } from 'vitest'
import { DEFAULT_ORGANIZATION_MODEL } from '../defaults'
import { DEFAULT_ORGANIZATION_MODEL_BRANDING, OrganizationModelBrandingSchema } from '../domains/branding'
import { DEFAULT_ORGANIZATION_MODEL_ENTITIES, EntitiesDomainSchema } from '../domains/entities'
import { DEFAULT_ORGANIZATION_MODEL_STATUSES, StatusesDomainSchema } from '../domains/statuses'
import { DEFAULT_ORGANIZATION_MODEL_SYSTEMS, SystemsDomainSchema } from '../domains/systems'
import { resolveOrganizationModel } from '../resolve'
import { OrganizationModelSchema } from '../schema'
import { DEFAULT_ORGANIZATION_MODEL_KNOWLEDGE } from '../defaults'

// Phase 4 (D8): sales, prospecting, projects, navigation top-level OM fields removed.
// DEFAULT_ORGANIZATION_MODEL_SALES / OrganizationModelSalesSchema,
// DEFAULT_ORGANIZATION_MODEL_PROJECTS / OrganizationModelProjectsSchema,
// DEFAULT_ORGANIZATION_MODEL_PROSPECTING / OrganizationModelProspectingSchema,
// DEFAULT_ORGANIZATION_MODEL_NAVIGATION / OrganizationModelNavigationSchema
// are no longer exported from their respective domain files.
// Tests that depended on these exports are skipped with explicit reason comments.

// Only the domain constants and schemas that still exist are covered here.
const domainCases = [
  {
    name: 'DEFAULT_ORGANIZATION_MODEL_BRANDING',
    constant: DEFAULT_ORGANIZATION_MODEL_BRANDING,
    schema: OrganizationModelBrandingSchema
  },
  {
    name: 'DEFAULT_ORGANIZATION_MODEL_ENTITIES',
    constant: DEFAULT_ORGANIZATION_MODEL_ENTITIES,
    schema: EntitiesDomainSchema
  },
  {
    name: 'DEFAULT_ORGANIZATION_MODEL_STATUSES',
    constant: DEFAULT_ORGANIZATION_MODEL_STATUSES,
    schema: StatusesDomainSchema
  },
  {
    name: 'DEFAULT_ORGANIZATION_MODEL_SYSTEMS',
    constant: DEFAULT_ORGANIZATION_MODEL_SYSTEMS,
    schema: SystemsDomainSchema
  }
] as const

describe('organization-model defaults', () => {
  describe('DEFAULT_ORGANIZATION_MODEL (composite)', () => {
    it('passes OrganizationModelSchema.parse without throwing', () => {
      expect(() => OrganizationModelSchema.parse(DEFAULT_ORGANIZATION_MODEL)).not.toThrow()
    })

    it('resolveOrganizationModel(DEFAULT_ORGANIZATION_MODEL) returns a schema-valid model', () => {
      const result = resolveOrganizationModel(DEFAULT_ORGANIZATION_MODEL)
      expect(() => OrganizationModelSchema.parse(result)).not.toThrow()
    })

    it('resolveOrganizationModel with no override equals resolveOrganizationModel with DEFAULT_ORGANIZATION_MODEL', () => {
      const fromUndefined = resolveOrganizationModel(undefined)
      const fromDefault = resolveOrganizationModel(DEFAULT_ORGANIZATION_MODEL)
      expect(fromDefault).toEqual(fromUndefined)
    })

    // Phase 4 (D8): model.navigation removed. The knowledge.command-view surface tracking
    // previously used DEFAULT_ORGANIZATION_MODEL_NAVIGATION.surfaces — skipped since that
    // constant is deleted. The system itself still exists; only the navigation surface
    // reference is gone from the top-level schema.
    it.skip('keeps Command View enabled but development-only in the default Knowledge navigation (deferred — Phase 4: navigation domain removed)', () => {
      // Previously checked DEFAULT_ORGANIZATION_MODEL_NAVIGATION.surfaces.
      // The command-view system still exists in DEFAULT_ORGANIZATION_MODEL.systems.
      // Re-enable with a top-level surfaces Record fixture if that assertion is needed.
    })
  })

  describe.each(domainCases)('$name (domain sub-constant)', ({ name: _name, constant, schema }) => {
    it('passes its domain schema parse without throwing', () => {
      expect(() => schema.parse(constant)).not.toThrow()
    })
  })

  // Phase 4 (D8): OrganizationModelSalesSchema, DEFAULT_ORGANIZATION_MODEL_SALES removed.
  describe.skip('DEFAULT_ORGANIZATION_MODEL_SALES via resolveOrganizationModel (deferred — Phase 4: sales domain removed)', () => {
    it('resolving with sales override produces a schema-valid model', () => {
      // Previously: resolveOrganizationModel({ sales: DEFAULT_ORGANIZATION_MODEL_SALES })
      // Sales data now lives in System.ontology.catalogTypes. Use getAllPipelines(model).
    })
  })

  // Phase 4 (D8): OrganizationModelProjectsSchema, DEFAULT_ORGANIZATION_MODEL_PROJECTS removed.
  describe.skip('DEFAULT_ORGANIZATION_MODEL_PROJECTS via resolveOrganizationModel (deferred — Phase 4: projects domain removed)', () => {
    it('resolving with projects override produces a schema-valid model', () => {
      // Previously: resolveOrganizationModel({ projects: DEFAULT_ORGANIZATION_MODEL_PROJECTS })
      // Project statuses now live in System.ontology.catalogTypes. Use getAllProjectStatuses(model).
    })
  })

  // Phase 4 (D8): OrganizationModelProspectingSchema, DEFAULT_ORGANIZATION_MODEL_PROSPECTING removed.
  describe.skip('DEFAULT_ORGANIZATION_MODEL_PROSPECTING via resolveOrganizationModel (deferred — Phase 4: prospecting domain removed)', () => {
    it('resolving with prospecting override produces a schema-valid model', () => {
      // Previously: resolveOrganizationModel({ prospecting: DEFAULT_ORGANIZATION_MODEL_PROSPECTING })
      // Prospecting stages/templates now live in System.ontology.catalogTypes.
    })
  })

  // Phase 4 (D8): OrganizationModelNavigationSchema, DEFAULT_ORGANIZATION_MODEL_NAVIGATION removed.
  describe.skip('DEFAULT_ORGANIZATION_MODEL_NAVIGATION via resolveOrganizationModel (deferred — Phase 4: navigation domain removed)', () => {
    it('resolving with navigation override produces a schema-valid model', () => {
      // Previously: resolveOrganizationModel({ navigation: DEFAULT_ORGANIZATION_MODEL_NAVIGATION })
      // Surfaces and navigation groups are now top-level Records on OrganizationModelSchemaBase.
    })
  })

  describe('DEFAULT_ORGANIZATION_MODEL_BRANDING via resolveOrganizationModel', () => {
    it('resolving with branding override produces a schema-valid model', () => {
      const result = resolveOrganizationModel({ branding: DEFAULT_ORGANIZATION_MODEL_BRANDING })
      expect(() => OrganizationModelSchema.parse(result)).not.toThrow()
    })
  })

  describe('DEFAULT_ORGANIZATION_MODEL_STATUSES via resolveOrganizationModel', () => {
    // Phase 4 (D1): statuses top-level field removed from OM schema.
    // DEFAULT_ORGANIZATION_MODEL_STATUSES is still exported from domains/statuses.ts
    // (it's used by the UI statuses domain as a semantic registry, not OM-schema-level data).
    // The resolveOrganizationModel override with { statuses: ... } was silently accepted
    // before Phase 4 because statuses was an extra field; now statuses is gone from OM entirely.
    // We keep the data-integrity checks below; the resolveOrganizationModel override test is skipped.
    it.skip('resolving with statuses override produces a schema-valid model (deferred — Phase 4: statuses field removed from OM schema)', () => {
      // Previously: resolveOrganizationModel({ statuses: DEFAULT_ORGANIZATION_MODEL_STATUSES })
      // Status data now lives in System.ontology.catalogTypes.
    })

    it('default statuses seed covers all 9 delivery.task values', () => {
      const taskEntries = Object.values(DEFAULT_ORGANIZATION_MODEL_STATUSES).filter(
        (e) => e.semanticClass === 'delivery.task'
      )
      expect(taskEntries).toHaveLength(9)
    })

    it('default statuses seed covers all 5 queue values', () => {
      const queueEntries = Object.values(DEFAULT_ORGANIZATION_MODEL_STATUSES).filter((e) => e.semanticClass === 'queue')
      expect(queueEntries).toHaveLength(5)
    })

    it('every entry has a non-empty id, label, and semanticClass', () => {
      for (const entry of Object.values(DEFAULT_ORGANIZATION_MODEL_STATUSES)) {
        expect(entry.id.length).toBeGreaterThan(0)
        expect(entry.label.length).toBeGreaterThan(0)
        expect(entry.semanticClass.length).toBeGreaterThan(0)
      }
    })

    it('all entry ids are unique', () => {
      const ids = Object.values(DEFAULT_ORGANIZATION_MODEL_STATUSES).map((e) => e.id)
      const uniqueIds = new Set(ids)
      expect(uniqueIds.size).toBe(ids.length)
    })
  })

  describe('DEFAULT_ORGANIZATION_MODEL_KNOWLEDGE — flat Record (Phase 4 D3)', () => {
    it('is an empty object (no pre-seeded nodes in generic base)', () => {
      expect(DEFAULT_ORGANIZATION_MODEL_KNOWLEDGE).toEqual({})
    })

    it('does not have a .nodes array (old wrapper shape removed)', () => {
      expect(DEFAULT_ORGANIZATION_MODEL_KNOWLEDGE).not.toHaveProperty('nodes')
    })

    it('does not have a .version field (moved to domainMetadata.knowledge)', () => {
      expect(DEFAULT_ORGANIZATION_MODEL_KNOWLEDGE).not.toHaveProperty('version')
    })
  })
})
