// ========= 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 type { S3Accessor } from '../../../accessor/s3.ts'
import { resolveGlob } from '../../../core/s3/glob.ts'
import { readdir as s3Readdir } from '../../../core/s3/readdir.ts'
import { rmR as s3RmR } from '../../../core/s3/rm.ts'
import { rmdir as s3Rmdir } from '../../../core/s3/rmdir.ts'
import { stat as s3Stat } from '../../../core/s3/stat.ts'
import { unlink as s3Unlink } from '../../../core/s3/unlink.ts'
import { IOResult, type ByteSource } from '../../../io/types.ts'
import { FileType, type PathSpec, ResourceName } from '../../../types.ts'
import { command, type CommandFnResult, type CommandOpts } from '../../config.ts'
import { specOf } from '../../spec/builtins.ts'

const ENC = new TextEncoder()

interface RmOpts {
  recursive: boolean
  force: boolean
  removeDir: boolean
}

async function rmOne(
  accessor: S3Accessor,
  path: PathSpec,
  opts: RmOpts,
  index: CommandOpts['index'],
): Promise<void> {
  let isDir = false
  try {
    const st = await s3Stat(accessor, path, index ?? undefined)
    isDir = st.type === FileType.DIRECTORY
  } catch (err) {
    if (opts.force) return
    throw err
  }
  if (isDir) {
    if (opts.recursive) {
      await s3RmR(accessor, path)
    } else if (opts.removeDir) {
      const children = await s3Readdir(accessor, path, index ?? undefined)
      if (children.length > 0) {
        throw new Error(`directory not empty: ${path.original}`)
      }
      await s3Rmdir(accessor, path)
    } else {
      throw new Error(`${path.original}: is a directory (use recursive=True)`)
    }
  } else {
    await s3Unlink(accessor, path)
  }
}

async function rmCommand(
  accessor: S3Accessor,
  paths: PathSpec[],
  _texts: string[],
  opts: CommandOpts,
): Promise<CommandFnResult> {
  if (paths.length === 0) {
    return [null, new IOResult({ exitCode: 1, stderr: ENC.encode('rm: missing operand\n') })]
  }
  const resolved = await resolveGlob(accessor, paths, opts.index ?? undefined)
  const recursive = opts.flags.r === true || opts.flags.R === true
  const force = opts.flags.f === true
  const removeDir = opts.flags.d === true
  const verbose = opts.flags.v === true
  const verboseParts: string[] = []
  const writes: Record<string, Uint8Array> = {}
  for (const p of resolved) {
    try {
      await rmOne(accessor, p, { recursive, force, removeDir }, opts.index)
    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err)
      return [null, new IOResult({ exitCode: 1, stderr: ENC.encode(`${msg}\n`) })]
    }
    writes[p.stripPrefix] = new Uint8Array()
    if (verbose) verboseParts.push(`removed '${p.original}'`)
  }
  const output: ByteSource | null = verbose ? ENC.encode(verboseParts.join('\n')) : null
  return [output, new IOResult({ writes })]
}

export const S3_RM = command({
  name: 'rm',
  resource: ResourceName.S3,
  spec: specOf('rm'),
  fn: rmCommand,
  write: true,
})
