use std::fs::{self, File, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};

use crate::replication::ShardId;

use super::error::WalError;

/// Format a segment file name from the shard ID and first sequence number.
///
/// For `ShardId::SINGLE` (shard 0), produces the v1 format:
///   `wal-00000000000000000001.seg`
///
/// For any other shard, produces the v2 format:
///   `wal-s00003-00000000000000000001.seg`
///
/// Zero-padded fields ensure lexicographic ordering matches numeric ordering.
#[must_use]
pub fn segment_filename(shard_id: ShardId, first_seq: u64) -> String {
    if shard_id == ShardId::SINGLE {
        format!("wal-{first_seq:020}.seg")
    } else {
        format!("wal-s{:05}-{:020}.seg", shard_id.0, first_seq)
    }
}

/// Parse the shard ID and first sequence number from a WAL segment filename.
///
/// Accepts both formats:
///   - `wal-{first_seq:020}.seg` (single-shard, v1) -> `(ShardId::SINGLE, first_seq)`
///   - `wal-s{shard_id:05}-{first_seq:020}.seg` (multi-shard, v2) -> `(ShardId(n), first_seq)`
///
/// Returns `None` if the filename does not match either format.
#[must_use]
pub fn parse_segment_filename(filename: &str) -> Option<(ShardId, u64)> {
    let name = filename.strip_suffix(".seg")?;

    // Multi-shard format: wal-s{shard_id}-{first_seq}
    if let Some(rest) = name.strip_prefix("wal-s") {
        let dash = rest.find('-')?;
        let shard_id: u16 = rest[..dash].parse().ok()?;
        let first_seq: u64 = rest[dash + 1..].parse().ok()?;
        return Some((ShardId(shard_id), first_seq));
    }

    // Single-shard format: wal-{first_seq}
    if let Some(seq_str) = name.strip_prefix("wal-") {
        let first_seq: u64 = seq_str.parse().ok()?;
        return Some((ShardId::SINGLE, first_seq));
    }

    None
}

/// Parse the first sequence number from a segment file name.
///
/// Backward-compatible wrapper around [`parse_segment_filename`] that discards
/// the shard ID. Used by [`list_segments`] and legacy callers.
///
/// Returns `None` if the file name does not match the expected pattern.
#[must_use]
pub fn parse_segment_seq(filename: &str) -> Option<u64> {
    parse_segment_filename(filename).map(|(_, seq)| seq)
}

/// List all WAL segment files in the directory, sorted by first sequence number.
///
/// # Errors
///
/// Returns `WalError::Io` on filesystem failure.
pub fn list_segments(dir: &Path) -> Result<Vec<(u64, PathBuf)>, WalError> {
    let mut segments = Vec::new();

    let entries = match fs::read_dir(dir) {
        Ok(e) => e,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(segments),
        Err(e) => return Err(WalError::Io(e)),
    };

    for entry in entries {
        let entry = entry?;
        let filename = entry.file_name();
        let Some(name) = filename.to_str() else {
            continue;
        };
        if let Some(seq) = parse_segment_seq(name) {
            segments.push((seq, entry.path()));
        }
    }

    segments.sort_by_key(|(seq, _)| *seq);
    Ok(segments)
}

/// List WAL segment files for a specific shard, sorted by first sequence number.
///
/// When `shard_id` is `ShardId::SINGLE`, includes all single-shard format segments.
/// When `shard_id > 0`, includes only segments with that shard prefix.
///
/// # Errors
///
/// Returns `WalError::Io` on filesystem failure.
pub fn list_segments_for_shard(
    dir: &Path,
    shard_id: ShardId,
) -> Result<Vec<(u64, PathBuf)>, WalError> {
    let mut segments = Vec::new();

    let entries = match fs::read_dir(dir) {
        Ok(e) => e,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(segments),
        Err(e) => return Err(WalError::Io(e)),
    };

    for entry in entries {
        let entry = entry?;
        let file_name = entry.file_name();
        let Some(name) = file_name.to_str() else {
            continue;
        };
        if let Some((seg_shard, seq)) = parse_segment_filename(name)
            && seg_shard == shard_id
        {
            segments.push((seq, entry.path()));
        }
    }

    segments.sort_by_key(|(seq, _)| *seq);
    Ok(segments)
}

/// Manages the current writable WAL segment file.
///
/// Handles creation of new segment files, tracking file size for rotation,
/// and data sync. Rotation is triggered externally by the writer when
/// the segment exceeds `max_size`.
pub struct SegmentWriter {
    dir: PathBuf,
    file: File,
    current_size: u64,
    max_size: u64,
    first_seq: u64,
    /// The last sequence number written to this segment.
    last_seq: u64,
    /// The shard this segment belongs to. Controls filename format:
    /// `ShardId::SINGLE` → v1 format, any other shard → v2 format.
    shard_id: ShardId,
}

impl SegmentWriter {
    /// Open or create a segment file for writing.
    ///
    /// `shard_id` controls the filename format:
    /// - `ShardId::SINGLE` (0) → v1 single-node format `wal-{seq:020}.seg`
    /// - Any other shard → v2 multi-shard format `wal-s{shard:05}-{seq:020}.seg`
    ///
    /// If `first_seq` identifies an existing segment, it is opened for append.
    /// Otherwise, a new file is created.
    ///
    /// # Errors
    ///
    /// Returns `WalError::Io` on filesystem failure.
    pub fn open(
        dir: &Path,
        shard_id: ShardId,
        first_seq: u64,
        max_size: u64,
    ) -> Result<Self, WalError> {
        let filename = segment_filename(shard_id, first_seq);
        let path = dir.join(&filename);
        let is_new = !path.exists();
        let file = OpenOptions::new().create(true).append(true).open(&path)?;

        // Fsync the parent directory so the new directory entry is durable.
        // Without this, a crash after file creation but before the directory
        // metadata is flushed could lose the segment file entirely.
        if is_new {
            let dir_fd = File::open(dir)?;
            dir_fd.sync_all()?;
        }

        let metadata = file.metadata()?;
        let current_size = metadata.len();

        Ok(Self {
            dir: dir.to_path_buf(),
            file,
            current_size,
            max_size,
            first_seq,
            last_seq: first_seq,
            shard_id,
        })
    }

    /// Write a raw batch of bytes to the current segment.
    ///
    /// Returns the file offset where the batch was written.
    ///
    /// # Errors
    ///
    /// Returns `WalError::Io` on write failure.
    pub fn write_batch_bytes(&mut self, bytes: &[u8]) -> Result<u64, WalError> {
        let offset = self.current_size;
        self.file.write_all(bytes)?;
        self.current_size += bytes.len() as u64;
        Ok(offset)
    }

    /// Sync all written data to stable storage.
    ///
    /// Uses `File::sync_data()` which maps to `fdatasync` on Linux and
    /// `fsync` on macOS. This is the safe Rust equivalent.
    ///
    /// # Errors
    ///
    /// Returns `WalError::Io` on sync failure.
    pub fn sync(&self) -> Result<(), WalError> {
        self.file.sync_data()?;
        Ok(())
    }

    /// Whether the segment has reached its size threshold and should be rotated.
    #[must_use]
    pub const fn needs_rotation(&self) -> bool {
        self.current_size >= self.max_size
    }

    /// The first sequence number in this segment.
    #[must_use]
    pub const fn first_seq(&self) -> u64 {
        self.first_seq
    }

    /// The last sequence number written to this segment.
    #[must_use]
    pub const fn last_seq(&self) -> u64 {
        self.last_seq
    }

    /// Update the last sequence number written to this segment.
    pub const fn set_last_seq(&mut self, seq: u64) {
        self.last_seq = seq;
    }

    /// The current file size in bytes.
    #[must_use]
    pub const fn current_size(&self) -> u64 {
        self.current_size
    }

    /// The shard ID used for segment filename formatting.
    #[must_use]
    pub const fn shard_id(&self) -> ShardId {
        self.shard_id
    }

    /// Create a new segment file and return a writer for it.
    ///
    /// Finalizes the current segment (syncs it) and opens a new one using the
    /// same `shard_id` that was set at construction time.
    ///
    /// # Errors
    ///
    /// Returns `WalError::Io` on filesystem failure.
    pub fn rotate(&mut self, new_first_seq: u64) -> Result<(), WalError> {
        // Sync current segment before rotation
        self.sync()?;

        let filename = segment_filename(self.shard_id, new_first_seq);
        let path = self.dir.join(&filename);
        let file = OpenOptions::new().create(true).append(true).open(&path)?;

        // Fsync the parent directory so the new segment's directory entry
        // is durable. Without this, a crash after file creation but before
        // the directory metadata is flushed could lose the new segment.
        let dir_fd = File::open(&self.dir)?;
        dir_fd.sync_all()?;

        self.file = file;
        self.current_size = 0;
        self.first_seq = new_first_seq;
        self.last_seq = new_first_seq;
        Ok(())
    }
}

/// Delete all segment files whose first sequence number is less than `before_seq`.
///
/// # Errors
///
/// Returns `WalError::Io` on filesystem failure. Partial deletion may occur
/// if an error is encountered mid-way.
pub fn delete_segments_before(dir: &Path, before_seq: u64) -> Result<usize, WalError> {
    let segments = list_segments(dir)?;
    let mut deleted = 0;
    for (seq, path) in segments {
        if seq < before_seq {
            fs::remove_file(&path)?;
            deleted += 1;
        }
    }
    Ok(deleted)
}

#[cfg(test)]
#[path = "segment_tests.rs"]
mod tests;
