import { BaseService, type BaseServiceContext } from '@learnitive/base-utils';
import { SERVICE_NAME } from '@learnitive/constants';
import {
  importProjectImage,
  type ImportProjectImageInput,
  type ProjectImageAsset,
} from '@learnitive/project-assets';
import type { ProjectCollaborationService } from './project-collaboration-service';
import type { ProjectStorageService } from './project-storage-service';

/** Stores user-owned project images through the workspace's configured project
 * storage provider. Source insertion is intentionally owned by the editor
 * layer; this service only makes the asset durable and returns its project path. */
export class ProjectAssetService extends BaseService {
  static deps = ['projectStorage', 'projectCollaboration'] as const;

  constructor(
    context: BaseServiceContext,
    private readonly dependencies: {
      projectStorage: ProjectStorageService;
      projectCollaboration: ProjectCollaborationService;
    },
  ) {
    super(SERVICE_NAME.projectAssetService, context, dependencies);
  }

  hookMount(): void {}

  async importImage(
    wsName: string,
    input: ImportProjectImageInput,
  ): Promise<ProjectImageAsset> {
    await this.mountPromise;
    this.dependencies.projectCollaboration.assertProjectStructureMutable(wsName, 'add project assets');
    const [storage, config] = await Promise.all([
      this.dependencies.projectStorage.getProvider(wsName),
      this.dependencies.projectStorage.getProjectConfig(wsName),
    ]);
    const asset = await importProjectImage(storage, config, input);
    return asset;
  }
}
