import type { IDataSource } from './datasource';
import type { Certificate, CertificateQueryParams } from './certificate';
import type { User, CreateUserDTO, UpdateUserDTO, UserQueryDTO } from '@shared/types';
import type { Product, CreateProductDTO, UpdateProductDTO, ProductQueryDTO } from '@shared/types';
import type { Order, CreateOrderDTO, UpdateOrderDTO, OrderQueryDTO } from '@shared/types';

export interface DataSourceMap {
  certificate: IDataSource<Certificate, CertificateQueryParams>;
  user: IDataSource<User, UserQueryDTO> & {
    create: (data: CreateUserDTO) => Promise<User>;
    update: (id: string, data: UpdateUserDTO) => Promise<User>;
  };
  product: IDataSource<Product, ProductQueryDTO> & {
    create: (data: CreateProductDTO) => Promise<Product>;
    update: (id: string, data: UpdateProductDTO) => Promise<Product>;
  };
  order: IDataSource<Order, OrderQueryDTO> & {
    create: (data: CreateOrderDTO) => Promise<Order>;
    update: (id: string, data: UpdateOrderDTO) => Promise<Order>;
  };
}

export type DataSourceKey = keyof DataSourceMap;

export type DataSourceType<K extends DataSourceKey> = DataSourceMap[K];

export function getDataSource<K extends DataSourceKey>(key: K): Promise<DataSourceType<K>> {
  const dataSources: Record<string, () => Promise<unknown>> = {
    certificate: () => import('../services/certificateDataSource').then(m => m.certificateDataSource),
    user: () => import('../services/userDataSource').then(m => m.userDataSource),
    product: () => import('../services/productDataSource').then(m => m.productDataSource),
    order: () => import('../services/orderDataSource').then(m => m.orderDataSource),
  };
  
  return dataSources[key]() as Promise<DataSourceType<K>>;
}
