// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========

import { normalizeKeyPrefix } from '@struktoai/mirage-core/resource/s3/config'
import { ResourceName } from '@struktoai/mirage-core/types'
import { HfModelsAccessor } from '../../accessor/hf.ts'
import { HfResource } from '../hf_buckets/base.ts'
import {
  assertHfRepoId,
  type HfRepoConfig,
  type HfRepoConfigRedacted,
  redactHfRepoConfig,
} from '../hf_buckets/config.ts'
import { HF_MODELS_PROMPT } from './prompt.ts'

export interface HfModelsResourceState {
  type: string
  config: HfRepoConfigRedacted
}

export class HfModelsResource extends HfResource {
  readonly kind: string = ResourceName.HF_MODELS
  readonly prompt: string = HF_MODELS_PROMPT
  readonly config: HfRepoConfig
  readonly accessor: HfModelsAccessor

  constructor(config: HfRepoConfig) {
    super()
    assertHfRepoId(config.repoId, 'repo_id')
    const normalized = normalizeKeyPrefix(config.keyPrefix)
    const cfg: HfRepoConfig = { ...config }
    if (normalized !== undefined) {
      cfg.keyPrefix = normalized
    } else {
      delete cfg.keyPrefix
    }
    this.config = cfg
    this.accessor = new HfModelsAccessor(this.config)
  }

  getState(): Promise<HfModelsResourceState> {
    return Promise.resolve({
      type: this.kind,
      config: redactHfRepoConfig(this.config),
    })
  }
}
