{-# OPTIONS_GHC -fno-warn-orphans #-}

module Network.Mail.Mime.Instances
  (
  ) where

import ClassyPrelude
import Network.Mail.Mime
import Data.Hashable (Hashable)

import Data.Aeson
import Data.Aeson.TH

import Utils.PathPiece

import Utils (assertM)

import qualified Data.Csv as Csv
  

deriving instance Read Address
deriving instance Ord Address
deriving instance Generic Address

instance Hashable Address
instance NFData Address

deriveToJSON defaultOptions
  { fieldLabelModifier = camelToPathPiece' 1
  } ''Address

instance FromJSON Address where
  parseJSON = withObject "Address" $ \obj -> do
    addressName <- assertM (not . null) <$> (obj .:? "name")
    addressEmail <- obj .: "email"
    return Address{..}


instance Csv.ToNamedRecord Address where
  toNamedRecord Address{..} = Csv.namedRecord
    [ "name" Csv..= addressName
    , "email" Csv..= addressEmail
    ]

instance Csv.DefaultOrdered Address where
  headerOrder _ = Csv.header [ "name", "email" ]
