-- Copyright 2026 Biglup Labs
-- SPDX-License-Identifier: Apache-2.0
--
-- 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
--
--     https://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.
{-# LANGUAGE OverloadedStrings #-}

-- | Convert between slots and Unix milliseconds within a period of constant
-- slot duration. A configuration applies from its reference slot onwards; it
-- does not describe earlier eras or predict future changes in slot duration.
--
-- @
-- import qualified Cometa.SlotConfig as Slots
--
-- firstMillisecond = Slots.slotToTime Slots.preview 100
-- enclosingSlot = firstMillisecond >>= Slots.timeToSlot Slots.preview
-- @
module Cometa.SlotConfig
  ( SlotConfig,
    mkSlotConfig,
    zeroTime,
    zeroSlot,
    slotLength,
    mainnet,
    preview,
    preprod,
    slotToTime,
    timeToSlot,
  )
where

-- IMPORTS --------------------------------------------------------------------

import Cometa.Error (Error (InvalidSlotConfig))
import Data.Word (Word64)

-- TYPES ----------------------------------------------------------------------

-- | A reference slot, its Unix start time, and a positive slot duration.
-- Equality compares these three values.
data SlotConfig = SlotConfig !Word64 !Word64 !Word64
  deriving (SlotConfig -> SlotConfig -> Bool
(SlotConfig -> SlotConfig -> Bool)
-> (SlotConfig -> SlotConfig -> Bool) -> Eq SlotConfig
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SlotConfig -> SlotConfig -> Bool
== :: SlotConfig -> SlotConfig -> Bool
$c/= :: SlotConfig -> SlotConfig -> Bool
/= :: SlotConfig -> SlotConfig -> Bool
Eq, Int -> SlotConfig -> ShowS
[SlotConfig] -> ShowS
SlotConfig -> String
(Int -> SlotConfig -> ShowS)
-> (SlotConfig -> String)
-> ([SlotConfig] -> ShowS)
-> Show SlotConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SlotConfig -> ShowS
showsPrec :: Int -> SlotConfig -> ShowS
$cshow :: SlotConfig -> String
show :: SlotConfig -> String
$cshowList :: [SlotConfig] -> ShowS
showList :: [SlotConfig] -> ShowS
Show)

-- DEFINITIONS ----------------------------------------------------------------

-- | Construct a constant-duration schedule. A zero duration is rejected.
mkSlotConfig ::
  -- | Unix start time of the reference slot, in milliseconds.
  Word64 ->
  -- | Reference slot number.
  Word64 ->
  -- | Duration of one slot, in milliseconds; must be positive.
  Word64 ->
  Either Error SlotConfig
mkSlotConfig :: Word64 -> Word64 -> Word64 -> Either Error SlotConfig
mkSlotConfig Word64
_ Word64
_ Word64
0 = Error -> Either Error SlotConfig
forall a b. a -> Either a b
Left (Text -> Error
InvalidSlotConfig Text
"Slot duration must be positive")
mkSlotConfig Word64
time Word64
slot Word64
duration = SlotConfig -> Either Error SlotConfig
forall a b. b -> Either a b
Right (Word64 -> Word64 -> Word64 -> SlotConfig
SlotConfig Word64
time Word64
slot Word64
duration)

-- | Unix start time of the reference slot, in milliseconds.
zeroTime :: SlotConfig -> Word64
zeroTime :: SlotConfig -> Word64
zeroTime (SlotConfig Word64
time Word64
_ Word64
_) = Word64
time

-- | First slot described by this configuration.
zeroSlot :: SlotConfig -> Word64
zeroSlot :: SlotConfig -> Word64
zeroSlot (SlotConfig Word64
_ Word64
slot Word64
_) = Word64
slot

-- | Positive duration of each slot, in milliseconds.
slotLength :: SlotConfig -> Word64
slotLength :: SlotConfig -> Word64
slotLength (SlotConfig Word64
_ Word64
_ Word64
duration) = Word64
duration

-- | Mainnet's one-second schedule starting at slot 4,492,800.
-- Earlier Byron slots require a different configuration.
mainnet :: SlotConfig
mainnet :: SlotConfig
mainnet = Word64 -> Word64 -> Word64 -> SlotConfig
SlotConfig Word64
1596059091000 Word64
4492800 Word64
1000

-- | Preview's one-second schedule starting at slot zero.
preview :: SlotConfig
preview :: SlotConfig
preview = Word64 -> Word64 -> Word64 -> SlotConfig
SlotConfig Word64
1666656000000 Word64
0 Word64
1000

-- | Preprod's one-second schedule starting at slot 86,400.
preprod :: SlotConfig
preprod :: SlotConfig
preprod = Word64 -> Word64 -> Word64 -> SlotConfig
SlotConfig Word64
1655769600000 Word64
86400 Word64
1000

-- | Return the Unix start time of a slot, in milliseconds. Slots before the
-- reference slot and times greater than 2^64 - 1 are rejected.
slotToTime :: SlotConfig -> Word64 -> Either Error Word64
slotToTime :: SlotConfig -> Word64 -> Either Error Word64
slotToTime SlotConfig
config Word64
slot
  | Word64
slot Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< SlotConfig -> Word64
zeroSlot SlotConfig
config = Error -> Either Error Word64
forall a b. a -> Either a b
Left (Text -> Error
InvalidSlotConfig Text
"Slot precedes the reference slot")
  | Bool
otherwise = Integer -> Either Error Word64
bounded (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (SlotConfig -> Word64
zeroTime SlotConfig
config) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
slot Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (SlotConfig -> Word64
zeroSlot SlotConfig
config)) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (SlotConfig -> Word64
slotLength SlotConfig
config))

-- | Return the slot containing a Unix timestamp in milliseconds. A slot includes
-- its start instant and excludes the next slot's start. Times before the reference
-- time and resulting slots greater than 2^64 - 1 are rejected.
timeToSlot :: SlotConfig -> Word64 -> Either Error Word64
timeToSlot :: SlotConfig -> Word64 -> Either Error Word64
timeToSlot SlotConfig
config Word64
time
  | Word64
time Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< SlotConfig -> Word64
zeroTime SlotConfig
config = Error -> Either Error Word64
forall a b. a -> Either a b
Left (Text -> Error
InvalidSlotConfig Text
"Time precedes the reference time")
  | Bool
otherwise = Integer -> Either Error Word64
bounded (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (SlotConfig -> Word64
zeroSlot SlotConfig
config) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
time Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (SlotConfig -> Word64
zeroTime SlotConfig
config)) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (SlotConfig -> Word64
slotLength SlotConfig
config))

bounded :: Integer -> Either Error Word64
bounded :: Integer -> Either Error Word64
bounded Integer
value
  | Integer
value Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64
forall a. Bounded a => a
maxBound :: Word64) = Error -> Either Error Word64
forall a b. a -> Either a b
Left (Text -> Error
InvalidSlotConfig Text
"Conversion exceeds the unsigned 64-bit range")
  | Bool
otherwise = Word64 -> Either Error Word64
forall a b. b -> Either a b
Right (Integer -> Word64
forall a. Num a => Integer -> a
fromInteger Integer
value)