// ========= 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 { mountKey, mountPrefixOf } from '../../utils/key_prefix.ts'
import type { SlackAccessor } from '../../accessor/slack.ts'
import type { IndexCacheStore } from '../../cache/index/store.ts'
import { FileStat, FileType, PathSpec } from '../../types.ts'
import { readdir as coreReaddir } from './readdir.ts'
import { epochToIso } from '../../utils/dates.ts'
import { filetypeFromMimetype } from '../../utils/filetype.ts'
import { stripSlash } from '../../utils/slash.ts'

const VIRTUAL_DIRS: ReadonlySet<string> = new Set(['', 'channels', 'dms', 'users'])
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/

function slackModified(remoteTime: string): string | null {
  if (remoteTime === '') return null
  const ts = Number.parseFloat(remoteTime)
  if (Number.isNaN(ts) || ts <= 0) return null
  return epochToIso(ts)
}

function fileNotFound(key: string): Error {
  const e = new Error(`ENOENT: ${key}`) as Error & { code: string }
  e.code = 'ENOENT'
  return e
}

async function lookupWithFallback(
  accessor: SlackAccessor,
  virtualKey: string,
  prefix: string,
  index: IndexCacheStore,
) {
  const result = await index.get(virtualKey)
  if (result.entry !== undefined && result.entry !== null) return result
  const parentVirtual = virtualKey.includes('/')
    ? virtualKey.slice(0, virtualKey.lastIndexOf('/')) || '/'
    : '/'
  try {
    await coreReaddir(
      accessor,
      new PathSpec({
        virtual: parentVirtual,
        directory: parentVirtual,
        resolved: false,
        resourcePath: mountKey(parentVirtual, prefix),
      }),
      index,
    )
  } catch {
    // parent listing failed — fall through
  }
  return await index.get(virtualKey)
}

export async function stat(
  accessor: SlackAccessor,
  path: PathSpec,
  index?: IndexCacheStore,
): Promise<FileStat> {
  const prefix = mountPrefixOf(path.virtual, path.resourcePath)
  let raw = path.virtual
  if (prefix !== '' && raw.startsWith(prefix)) {
    raw = raw.slice(prefix.length) || '/'
  }
  const key = stripSlash(raw)

  if (VIRTUAL_DIRS.has(key)) {
    const name = key !== '' ? key : '/'
    return new FileStat({ name, type: FileType.DIRECTORY })
  }

  const parts = key.split('/')
  const part0 = parts[0] ?? ''
  const part2 = parts[2] ?? ''
  const part3 = parts[3] ?? ''
  const virtualKey = `${prefix}/${key}`

  if (parts.length === 2 && (part0 === 'channels' || part0 === 'dms')) {
    if (index === undefined) throw fileNotFound(raw)
    const lookup = await lookupWithFallback(accessor, virtualKey, prefix, index)
    if (lookup.entry === undefined || lookup.entry === null) {
      throw fileNotFound(raw)
    }
    return new FileStat({
      name: lookup.entry.vfsName !== '' ? lookup.entry.vfsName : lookup.entry.name,
      type: FileType.DIRECTORY,
      modified: slackModified(lookup.entry.remoteTime),
      extra: { channel_id: lookup.entry.id },
    })
  }

  if (parts.length === 2 && part0 === 'users') {
    if (index === undefined) throw fileNotFound(raw)
    const lookup = await lookupWithFallback(accessor, virtualKey, prefix, index)
    if (lookup.entry === undefined || lookup.entry === null) {
      throw fileNotFound(raw)
    }
    return new FileStat({
      name: lookup.entry.vfsName !== '' ? lookup.entry.vfsName : lookup.entry.name,
      type: FileType.JSON,
      extra: { user_id: lookup.entry.id },
    })
  }

  if (parts.length === 3 && (part0 === 'channels' || part0 === 'dms') && DATE_RE.test(part2)) {
    return new FileStat({ name: part2, type: FileType.DIRECTORY })
  }

  if (
    parts.length === 4 &&
    (part0 === 'channels' || part0 === 'dms') &&
    DATE_RE.test(part2) &&
    part3 === 'chat.jsonl'
  ) {
    return new FileStat({ name: 'chat.jsonl', type: FileType.TEXT })
  }

  if (
    parts.length === 4 &&
    (part0 === 'channels' || part0 === 'dms') &&
    DATE_RE.test(part2) &&
    part3 === 'files'
  ) {
    return new FileStat({ name: 'files', type: FileType.DIRECTORY })
  }

  if (
    parts.length === 5 &&
    (part0 === 'channels' || part0 === 'dms') &&
    DATE_RE.test(part2) &&
    part3 === 'files'
  ) {
    if (index === undefined) throw fileNotFound(raw)
    const lookup = await lookupWithFallback(accessor, virtualKey, prefix, index)
    if (lookup.entry === undefined || lookup.entry === null) {
      throw fileNotFound(raw)
    }
    const mimetype =
      typeof lookup.entry.extra.mimetype === 'string' ? lookup.entry.extra.mimetype : ''
    return new FileStat({
      name: lookup.entry.vfsName !== '' ? lookup.entry.vfsName : lookup.entry.name,
      type: filetypeFromMimetype(mimetype),
      size: lookup.entry.size ?? null,
      modified: slackModified(lookup.entry.remoteTime),
      extra: { file_id: lookup.entry.id },
    })
  }

  throw fileNotFound(raw)
}
