import { Container } from '@novadi/core';
import { IRenderer } from './core/IGroupingRenderer';
import { IGroupingStore } from './core/IGroupingStore';
import { DateRenderer } from './features/date/DateRenderer';
import { DateService } from './core/DateService';
import { ITimeFormatConfig } from './core/ITimeFormatConfig';
import { IGridConfig } from './core/IGridConfig';
import { ResourceRenderer } from './features/resource/ResourceRenderer';
import { TeamRenderer } from './features/team/TeamRenderer';
import { DepartmentRenderer } from './features/department/DepartmentRenderer';
import { CalendarOrchestrator } from './core/CalendarOrchestrator';
import { CalendarApp } from './core/CalendarApp';
import { TimeAxisRenderer } from './features/timeaxis/TimeAxisRenderer';
import { ScrollManager } from './core/ScrollManager';
import { HeaderDrawerManager } from './core/HeaderDrawerManager';
import { MockTeamStore, MockResourceStore } from './demo/MockStores';
import { DemoApp } from './demo/DemoApp';

// Event system
import { EventBus } from './core/EventBus';
import { IEventBus, ICalendarEvent, ISync, IResource, IBooking, ICustomer, ITeam, IDepartment } from './types/CalendarTypes';

// Storage
import { IndexedDBContext } from './storage/IndexedDBContext';
import { IStore } from './storage/IStore';
import { IEntityService } from './storage/IEntityService';
import { EventStore } from './storage/events/EventStore';
import { EventService } from './storage/events/EventService';
import { ResourceStore } from './storage/resources/ResourceStore';
import { ResourceService } from './storage/resources/ResourceService';
import { BookingStore } from './storage/bookings/BookingStore';
import { BookingService } from './storage/bookings/BookingService';
import { CustomerStore } from './storage/customers/CustomerStore';
import { CustomerService } from './storage/customers/CustomerService';
import { TeamStore } from './storage/teams/TeamStore';
import { TeamService } from './storage/teams/TeamService';
import { DepartmentStore } from './storage/departments/DepartmentStore';
import { DepartmentService } from './storage/departments/DepartmentService';
import { SettingsStore } from './storage/settings/SettingsStore';
import { SettingsService } from './storage/settings/SettingsService';
import { TenantSetting } from './types/SettingsTypes';
import { ViewConfigStore } from './storage/viewconfigs/ViewConfigStore';
import { ViewConfigService } from './storage/viewconfigs/ViewConfigService';
import { ViewConfig } from './core/ViewConfig';

// Audit
import { AuditStore } from './storage/audit/AuditStore';
import { AuditService } from './storage/audit/AuditService';
import { IAuditEntry } from './types/AuditTypes';

// Repositories
import { IApiRepository } from './repositories/IApiRepository';
import { MockEventRepository } from './repositories/MockEventRepository';
import { MockResourceRepository } from './repositories/MockResourceRepository';
import { MockBookingRepository } from './repositories/MockBookingRepository';
import { MockCustomerRepository } from './repositories/MockCustomerRepository';
import { MockAuditRepository } from './repositories/MockAuditRepository';
import { MockTeamRepository } from './repositories/MockTeamRepository';
import { MockDepartmentRepository } from './repositories/MockDepartmentRepository';
import { MockSettingsRepository } from './repositories/MockSettingsRepository';
import { MockViewConfigRepository } from './repositories/MockViewConfigRepository';

// Workers
import { DataSeeder } from './workers/DataSeeder';

// Features
import { EventRenderer } from './features/event/EventRenderer';
import { ScheduleRenderer } from './features/schedule/ScheduleRenderer';
import { HeaderDrawerRenderer } from './features/headerdrawer/HeaderDrawerRenderer';

// Schedule
import { ScheduleOverrideStore } from './storage/schedules/ScheduleOverrideStore';
import { ScheduleOverrideService } from './storage/schedules/ScheduleOverrideService';
import { ResourceScheduleService } from './storage/schedules/ResourceScheduleService';

// Managers
import { DragDropManager } from './managers/DragDropManager';
import { EdgeScrollManager } from './managers/EdgeScrollManager';
import { ResizeManager } from './managers/ResizeManager';
import { EventPersistenceManager } from './managers/EventPersistenceManager';

const defaultTimeFormatConfig: ITimeFormatConfig = {
  timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
  use24HourFormat: true,
  locale: 'da-DK',
  dateFormat: 'locale',
  showSeconds: false
};

const defaultGridConfig: IGridConfig = {
  hourHeight: 64,
  dayStartHour: 6,
  dayEndHour: 18,
  snapInterval: 15,
  gridStartThresholdMinutes: 30
};

export function createContainer(): Container {
  const container = new Container();
  const builder = container.builder();

  // Config
  builder.registerInstance(defaultTimeFormatConfig).as<ITimeFormatConfig>();
  builder.registerInstance(defaultGridConfig).as<IGridConfig>();

  // Core - EventBus
  builder.registerType(EventBus).as<EventBus>();
  builder.registerType(EventBus).as<IEventBus>();

  // Services
  builder.registerType(DateService).as<DateService>();

  // Storage infrastructure
  builder.registerType(IndexedDBContext).as<IndexedDBContext>();

  // Stores (for IndexedDB schema creation)
  builder.registerType(EventStore).as<IStore>();
  builder.registerType(ResourceStore).as<IStore>();
  builder.registerType(BookingStore).as<IStore>();
  builder.registerType(CustomerStore).as<IStore>();
  builder.registerType(TeamStore).as<IStore>();
  builder.registerType(DepartmentStore).as<IStore>();
  builder.registerType(ScheduleOverrideStore).as<IStore>();
  builder.registerType(AuditStore).as<IStore>();
  builder.registerType(SettingsStore).as<IStore>();
  builder.registerType(ViewConfigStore).as<IStore>();

  // Entity services (for DataSeeder polymorphic array)
  builder.registerType(EventService).as<IEntityService<ICalendarEvent>>();
  builder.registerType(EventService).as<IEntityService<ISync>>();
  builder.registerType(EventService).as<EventService>();

  builder.registerType(ResourceService).as<IEntityService<IResource>>();
  builder.registerType(ResourceService).as<IEntityService<ISync>>();
  builder.registerType(ResourceService).as<ResourceService>();

  builder.registerType(BookingService).as<IEntityService<IBooking>>();
  builder.registerType(BookingService).as<IEntityService<ISync>>();
  builder.registerType(BookingService).as<BookingService>();

  builder.registerType(CustomerService).as<IEntityService<ICustomer>>();
  builder.registerType(CustomerService).as<IEntityService<ISync>>();
  builder.registerType(CustomerService).as<CustomerService>();

  builder.registerType(TeamService).as<IEntityService<ITeam>>();
  builder.registerType(TeamService).as<IEntityService<ISync>>();
  builder.registerType(TeamService).as<TeamService>();

  builder.registerType(DepartmentService).as<IEntityService<IDepartment>>();
  builder.registerType(DepartmentService).as<IEntityService<ISync>>();
  builder.registerType(DepartmentService).as<DepartmentService>();

  builder.registerType(SettingsService).as<IEntityService<TenantSetting>>();
  builder.registerType(SettingsService).as<IEntityService<ISync>>();
  builder.registerType(SettingsService).as<SettingsService>();

  builder.registerType(ViewConfigService).as<IEntityService<ViewConfig>>();
  builder.registerType(ViewConfigService).as<IEntityService<ISync>>();
  builder.registerType(ViewConfigService).as<ViewConfigService>();

  // Repositories (for DataSeeder polymorphic array)
  builder.registerType(MockEventRepository).as<IApiRepository<ICalendarEvent>>();
  builder.registerType(MockEventRepository).as<IApiRepository<ISync>>();

  builder.registerType(MockResourceRepository).as<IApiRepository<IResource>>();
  builder.registerType(MockResourceRepository).as<IApiRepository<ISync>>();

  builder.registerType(MockBookingRepository).as<IApiRepository<IBooking>>();
  builder.registerType(MockBookingRepository).as<IApiRepository<ISync>>();

  builder.registerType(MockCustomerRepository).as<IApiRepository<ICustomer>>();
  builder.registerType(MockCustomerRepository).as<IApiRepository<ISync>>();

  builder.registerType(MockAuditRepository).as<IApiRepository<IAuditEntry>>();
  builder.registerType(MockAuditRepository).as<IApiRepository<ISync>>();

  builder.registerType(MockTeamRepository).as<IApiRepository<ITeam>>();
  builder.registerType(MockTeamRepository).as<IApiRepository<ISync>>();

  builder.registerType(MockDepartmentRepository).as<IApiRepository<IDepartment>>();
  builder.registerType(MockDepartmentRepository).as<IApiRepository<ISync>>();

  builder.registerType(MockSettingsRepository).as<IApiRepository<TenantSetting>>();
  builder.registerType(MockSettingsRepository).as<IApiRepository<ISync>>();

  builder.registerType(MockViewConfigRepository).as<IApiRepository<ViewConfig>>();
  builder.registerType(MockViewConfigRepository).as<IApiRepository<ISync>>();

  // Audit service (listens to ENTITY_SAVED/DELETED events automatically)
  builder.registerType(AuditService).as<AuditService>();

  // Workers
  builder.registerType(DataSeeder).as<DataSeeder>();

  // Schedule services
  builder.registerType(ScheduleOverrideService).as<ScheduleOverrideService>();
  builder.registerType(ResourceScheduleService).as<ResourceScheduleService>();

  // Features
  builder.registerType(EventRenderer).as<EventRenderer>();
  builder.registerType(ScheduleRenderer).as<ScheduleRenderer>();
  builder.registerType(HeaderDrawerRenderer).as<HeaderDrawerRenderer>();

  // Renderers - registreres som Renderer (array injection til CalendarOrchestrator)
  builder.registerType(DateRenderer).as<IRenderer>();
  builder.registerType(ResourceRenderer).as<IRenderer>();
  builder.registerType(TeamRenderer).as<IRenderer>();
  builder.registerType(DepartmentRenderer).as<IRenderer>();

  // Stores - registreres som IGroupingStore
  builder.registerType(MockTeamStore).as<IGroupingStore>();
  builder.registerType(MockResourceStore).as<IGroupingStore>();

  // CalendarOrchestrator modtager IGroupingStore[] automatisk (array injection)
  builder.registerType(CalendarOrchestrator).as<CalendarOrchestrator>();
  builder.registerType(TimeAxisRenderer).as<TimeAxisRenderer>();
  builder.registerType(ScrollManager).as<ScrollManager>();
  builder.registerType(HeaderDrawerManager).as<HeaderDrawerManager>();
  builder.registerType(DragDropManager).as<DragDropManager>();
  builder.registerType(EdgeScrollManager).as<EdgeScrollManager>();
  builder.registerType(ResizeManager).as<ResizeManager>();
  builder.registerType(EventPersistenceManager).as<EventPersistenceManager>();

  // CalendarApp - genbrugelig kalenderkomponent
  builder.registerType(CalendarApp).as<CalendarApp>();

  // Demo app
  builder.registerType(DemoApp).as<DemoApp>();

  return builder.build();
}
