// ========= 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 { stream as ramStream } from '../../../../core/ram/stream.ts'
import type { RAMAccessor } from '../../../../accessor/ram.ts'
import { tail as hdf5Tail } from '../../../../core/filetype/hdf5.ts'
import { ResourceName, type PathSpec } from '../../../../types.ts'
import { IOResult, materialize, type ByteSource } from '../../../../io/types.ts'
import { command, type CommandFnResult, type CommandOpts } from '../../../config.ts'
import { specOf } from '../../../spec/builtins.ts'

const ENC = new TextEncoder()

async function tailHdf5Command(
  accessor: RAMAccessor,
  paths: PathSpec[],
  texts: string[],
  opts: CommandOpts,
): Promise<CommandFnResult> {
  if (paths.length === 0) {
    return [null, new IOResult({ exitCode: 1, stderr: ENC.encode('tail: missing operand\n') })]
  }
  const first = paths[0]
  if (first === undefined) return [null, new IOResult()]
  const n = typeof opts.flags.n === 'string' ? Number.parseInt(opts.flags.n, 10) : 10
  try {
    const raw = await materialize(ramStream(accessor, first))
    const result = await hdf5Tail(raw, n)
    const out: ByteSource = result
    return [out, new IOResult({ cache: [first.stripPrefix] })]
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err)
    return [
      null,
      new IOResult({
        exitCode: 1,
        stderr: ENC.encode(`tail: ${first.original}: failed to read as hdf5: ${msg}\n`),
      }),
    ]
  }
}

export const RAM_TAIL_HDF5 = command({
  name: 'tail',
  resource: ResourceName.RAM,
  spec: specOf('tail'),
  filetype: '.h5',
  fn: tailHdf5Command,
})
