namespace NubLang.Code;

public readonly struct SourceLocation : IEquatable<SourceLocation>, IComparable<SourceLocation>
{
    public static SourceLocation Zero => new(0, 0);

    public SourceLocation(int line, int column)
    {
        Line = line;
        Column = column;
    }

    public int Line { get; }
    public int Column { get; }

    public override string ToString()
    {
        return $"{Line}:{Column}";
    }

    public override bool Equals(object? obj)
    {
        return obj is SourceLocation other && Equals(other);
    }

    public bool Equals(SourceLocation other)
    {
        return Line == other.Line && Column == other.Column;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(typeof(SourceLocation), Line, Column);
    }

    public static bool operator ==(SourceLocation left, SourceLocation right) => Equals(left, right);
    public static bool operator !=(SourceLocation left, SourceLocation right) => !Equals(left, right);
    public static bool operator <(SourceLocation left, SourceLocation right) => left.Line < right.Line || (left.Line == right.Line && left.Column < right.Column);
    public static bool operator >(SourceLocation left, SourceLocation right) => left.Line > right.Line || (left.Line == right.Line && left.Column > right.Column);
    public static bool operator <=(SourceLocation left, SourceLocation right) => left.Line <= right.Line || (left.Line == right.Line && left.Column <= right.Column);
    public static bool operator >=(SourceLocation left, SourceLocation right) => left.Line >= right.Line || (left.Line == right.Line && left.Column >= right.Column);

    public int CompareTo(SourceLocation other)
    {
        var lineComparison = Line.CompareTo(other.Line);
        return lineComparison != 0 ? lineComparison : Column.CompareTo(other.Column);
    }
}