about summary refs log tree commit diff
path: root/users/Profpatsch/whatcd-resolver/src/WhatcdResolver.hs
blob: c33ddd62d6ca93a98171bcfd9e27e137158ded61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE QuasiQuotes #-}

module WhatcdResolver where

import Control.Category qualified as Cat
import Control.Monad.Logger qualified as Logger
import Control.Monad.Logger.CallStack
import Control.Monad.Reader
import Data.Aeson qualified as Json
import Data.Aeson.BetterErrors qualified as Json
import Data.Aeson.KeyMap qualified as KeyMap
import Data.Error.Tree
import Data.List.NonEmpty qualified as NonEmpty
import Data.Map.Strict qualified as Map
import Data.Pool (Pool)
import Data.Pool qualified as Pool
import Data.Text qualified as Text
import Database.PostgreSQL.Simple (Only (..))
import Database.PostgreSQL.Simple qualified as Postgres
import Database.PostgreSQL.Simple.SqlQQ (sql)
import Database.PostgreSQL.Simple.Types (PGArray (PGArray))
import Database.PostgreSQL.Simple.Types qualified as Postgres
import Database.Postgres.Temp qualified as TmpPg
import FieldParser qualified as Field
import GHC.Records (HasField (..))
import IHP.HSX.QQ (hsx)
import Json qualified
import Json.Enc (Enc)
import Json.Enc qualified as Enc
import Label
import Multipart2 qualified as Multipart
import Network.HTTP.Conduit qualified as Http
import Network.HTTP.Simple qualified as Http
import Network.HTTP.Types
import Network.HTTP.Types qualified as Http
import Network.Wai qualified as Wai
import Network.Wai.Handler.Warp qualified as Warp
import PossehlAnalyticsPrelude
import Postgres.Decoder qualified as Dec
import Postgres.MonadPostgres
import Pretty
import RunCommand
import System.Directory qualified as Dir
import System.Directory qualified as Xdg
import System.FilePath ((</>))
import System.IO qualified as IO
import Text.Blaze.Html (Html, (!))
import Text.Blaze.Html.Renderer.Utf8 qualified as Html
import Text.Blaze.Html5 qualified as Html
import Text.Blaze.Html5.Attributes qualified as Attr
import UnliftIO

htmlUi :: App ()
htmlUi = do
  withRunInIO $ \runInIO -> Warp.run 8080 $ \req resp -> do
    let h act = do
          res <- runInIO act
          resp . Wai.responseLBS Http.ok200 [("Content-Type", "text/html")] . Html.renderHtml $ res
    let mp parser =
          Multipart.parseMultipartOrThrow
            appThrowTree
            parser
            req

    case req & Wai.pathInfo & Text.intercalate "/" of
      "" -> h mainHtml
      "snips/song" -> h snipsSong
      "snips/redacted/search" -> do
        h $ do
          dat <-
            mp
              ( do
                  label @"searchstr" <$> Multipart.field "redacted-search" Cat.id
              )
          snipsRedactedSearch dat
      "snips/redacted/torrentDataJson" -> h $ do
        dat <-
          mp
            ( do
                label @"id" <$> Multipart.field "torrent-id" ((Field.utf8 >>> Field.signedDecimal >>> Field.bounded @Int "int"))
            )
        mkVal <$> (runTransaction $ getTorrentById dat)
      _ -> h mainHtml
  where
    tableData =
      ( [ "Group ID",
          "Torrent ID",
          "Artist",
          "Name",
          "Weight",
          "Torrent"
        ],
        \t ->
          [ Enc.int t.groupId,
            Enc.int t.torrentId,
            Enc.text t.torrentGroupJson.artist,
            Enc.text t.torrentGroupJson.groupName,
            Enc.int t.seedingWeight,
            Enc.value t.torrentJson
          ]
      )

    mkTable :: ([Text], t -> [Enc]) -> [t] -> Html
    mkTable f ts = do
      let headers = Html.thead (fst f <&> Html.toHtml @Text <&> Html.th & mconcat)
      let keys = fst f <&> Text.toLower <&> Text.replace " " "_"
      let json = Enc.list (\t -> Enc.object (zip keys (t & snd f))) ts
      let tableDataScript =
            Html.script
              ! Attr.type_ "application/json"
              ! Attr.id "table-data"
              $ (json & Enc.encToBytesUtf8 & bytesToTextUtf8Unsafe & Html.text)
      [hsx|
        {tableDataScript}
        <table id="table" class="table">
          {headers}
          <tbody>
          </tbody>
        </table>
        <script>
          var tableData = JSON.parse($("#table-data").text());
          $("table").dynatable({
            dataset: {
              records: tableData
            }
          } )
        </script>
      |]

    mainHtml = runTransaction $ do
      bestTorrents <- getBestTorrents
      pure $
        Html.docTypeHtml
          [hsx|
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js" integrity="sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/Dynatable/0.3.1/jquery.dynatable.min.js" integrity="sha512-KJdW8vGZWsRYrhMlZ6d8dR/fbLBK/aPOI0xDTEnGysk8TiFffc0S6TLSeSg7Lzk84GhBu9wI+qQatBrnTAeEYQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
        <script src="https://unpkg.com/htmx.org@1.9.2" integrity="sha384-L6OqL9pRWyyFU3+/bjdSri+iIphTN/bvYyM37tICVyOJkWZLpP2vGn6VUEXgzg6h" crossorigin="anonymous"></script>
        <script>
          $.dynatableSetup({
            table: {
              defaultColumnIdStyle: 'underscore'
            }
          });
        </script>
      </head>
      <body>
        <form
          hx-post="/snips/redacted/search"
          hx-target="#redacted-search-results">
          <label for="redacted-search">Redacted Search</label>
          <input
            id="redacted-search"
            type="text"
            name="redacted-search" />
          <button type="submit">Search</button>
        </form>
        <div id="redacted-search-results"></div>
        {mkTable tableData bestTorrents}
      </body>
    |]
    snipsSong = todo

snipsRedactedSearch ::
  ( MonadLogger m,
    MonadIO m,
    MonadThrow m,
    MonadPostgres m,
    HasField "searchstr" r ByteString
  ) =>
  r ->
  m Html
snipsRedactedSearch dat = do
  t <-
    redactedSearchAndInsert
      [ ("searchstr", dat.searchstr)
      ]
  best :: [TorrentData] <- runTransaction $ do
    t
    getBestTorrents
  let bestRows =
        best
          & foldMap
            ( \b -> do
                [hsx|
                  <tr>
                  <td>{Html.toHtml @Int b.groupId}</td>
                  <td>{Html.toHtml @Text b.torrentGroupJson.artist}</td>
                  <td>{Html.toHtml @Text b.torrentGroupJson.groupName}</td>
                  <td>{Html.toHtml @Int b.seedingWeight}</td>
                  <td><details hx-post="snips/redacted/torrentDataJson" hx-vals={Enc.encToBytesUtf8 $ Enc.object [("torrent-id", Enc.int b.torrentIdDb)]}></details></td>
                  </tr>
                |]
            )
  pure $
    [hsx|
        <table class="table">
          <thead>
            <th>Group ID</th>
            <th>Artist</th>
            <th>Name</th>
            <th>Weight</th>
            <th>Torrent</th>
            <th>Torrent Group</th>
          </thead>
          <tbody>
            {bestRows}
          </tbody>
        </table>
      |]

mkVal :: Json.Value -> Html
mkVal = \case
  Json.Number n -> Html.toHtml @Text $ showToText n
  Json.String s -> Html.toHtml @Text s
  Json.Bool True -> [hsx|<em>true</em>|]
  Json.Bool False -> [hsx|<em>false</em>|]
  Json.Null -> [hsx|<em>null</em>|]
  Json.Array arr ->
    arr
      & foldMap (\el -> Html.ul $ mkVal el)
      & Html.ol
  Json.Object obj ->
    obj
      & KeyMap.toMapText
      & Map.toList
      & foldMap (\(k, v) -> Html.dt (Html.toHtml @Text k <> Html.dd (mkVal v)))
      & Html.dl

toTable :: [[(Text, Json.Value)]] -> Html
toTable xs =
  case xs & nonEmpty of
    Nothing ->
      [hsx|<p>No results.</p>|]
    Just xs' -> do
      let headers = xs' & NonEmpty.head <&> fst <&> (\h -> [hsx|<th>{h}</th>|]) & mconcat
      let vals = xs' <&> fmap (mkVal . snd)
      [hsx|
              <table class="table">
                <thead>
                  {headers}
                </thead>
                <tbody>
                  {vals}
                </tbody>
              </table>
          |]

data TransmissionRequest = TransmissionRequest
  { method :: Text,
    arguments :: Map Text Enc,
    tag :: Maybe Int
  }
  deriving stock (Show)

testTransmission :: TransmissionRequest -> IO (Either TmpPg.StartError ())
testTransmission req = runAppWith $ doTransmissionRequest (T2 (label @"host" "localhost") (label @"port" "9091")) req >>= liftIO . printPretty

requestListAllTorrents :: TransmissionRequest
requestListAllTorrents =
  TransmissionRequest
    { method = "torrent-get",
      arguments =
        Map.fromList
          [ ("fields", Enc.list Enc.text ["id", "name", "files", "fileStats"])
          ],
      tag = Nothing
    }

data TransmissionResponse = TransmissionResponse
  { result :: TransmissionResponseStatus,
    arguments :: Map Text Json.Value,
    tag :: Maybe Int
  }
  deriving stock (Show)

data TransmissionResponseStatus
  = TransmissionResponseSuccess
  | TransmissionResponseFailure Text
  deriving stock (Show)

doTransmissionRequest ::
  ( MonadIO m,
    MonadTransmission m,
    HasField "host" t1 Text,
    HasField "port" t1 Text,
    MonadThrow m
  ) =>
  t1 ->
  TransmissionRequest ->
  m TransmissionResponse
doTransmissionRequest dat req = do
  sessionId <- getTransmissionId
  let httpReq =
        [fmt|http://{dat.host}:{dat.port}/transmission/rpc|]
          & Http.setRequestMethod "POST"
          & Http.setRequestBodyLBS
            ( Enc.encToBytesUtf8Lazy $
                Enc.object
                  ( [ ("method", req.method & Enc.text),
                      ("arguments", Enc.map id req.arguments)
                    ]
                      <> (req.tag & maybe [] (\t -> [("tag", t & Enc.int)]))
                  )
            )
          & (sessionId & maybe id (Http.setRequestHeader "X-Transmission-Session-Id" . (: [])))
  resp <- Http.httpBS httpReq
  -- Implement the CSRF protection thingy
  case resp & Http.getResponseStatus & (.statusCode) of
    409 -> do
      tid <-
        resp
          & Http.getResponseHeader "X-Transmission-Session-Id"
          & nonEmpty
          & annotate [fmt|Missing "X-Transmission-Session-Id" header in 409 response: {showPretty resp}|]
          & unwrapIOError
          & liftIO
          <&> NonEmpty.head
      setTransmissionId tid
      doTransmissionRequest dat req
    200 ->
      resp
        & Http.getResponseBody
        & Json.parseStrict
          ( Json.mapError singleError $ do
              result <-
                Json.key "result" Json.asText <&> \case
                  "success" -> TransmissionResponseSuccess
                  err -> TransmissionResponseFailure err
              arguments <-
                Json.keyMay "arguments" Json.asObject
                  <&> fromMaybe mempty
                  <&> KeyMap.toMapText
              tag <-
                Json.keyMay
                  "tag"
                  (Field.jsonParser (Field.jsonNumber >>> Field.boundedScientificIntegral "tag too long"))
              pure TransmissionResponse {..}
          )
        & first (Json.parseErrorTree "Cannot parse transmission RPC response")
        & \case
          Right a -> pure a
          Left err -> appThrowTree err
    _ -> liftIO $ unwrapIOError $ Left [fmt|Non-200 response: {showPretty resp}|]

redactedSearch ::
  (MonadLogger m, MonadIO m, MonadThrow m) =>
  [(ByteString, ByteString)] ->
  Json.Parse ErrorTree a ->
  m a
redactedSearch advanced =
  redactedApiRequest
    ( T2
        (label @"action" "browse")
        (label @"actionArgs" ((advanced <&> second Just)))
    )

test :: Bool -> IO (Either TmpPg.StartError ())
test doSearch =
  runAppWith $ do
    _ <- runTransaction migrate
    when doSearch $ do
      transaction <- bla
      _ <- runTransaction transaction
      pure ()
    htmlUi

-- fix
--   ( \io -> do
--       logInfo "delay"
--       liftIO $ threadDelay 10_000_000
--       io
--   )

bla :: (MonadThrow m, MonadIO m, MonadLogger m, MonadPostgres m) => m (Transaction m ())
bla = do
  t1 <-
    redactedSearchAndInsert
      [ ("searchstr", "cherish"),
        ("artistname", "kirinji"),
        -- ("year", "1982"),
        -- ("format", "MP3"),
        -- ("releasetype", "album"),
        ("order_by", "year")
      ]
  t3 <-
    redactedSearchAndInsert
      [ ("searchstr", "mouss et hakim"),
        ("artistname", "mouss et hakim"),
        -- ("year", "1982"),
        -- ("format", "MP3"),
        -- ("releasetype", "album"),
        ("order_by", "year")
      ]
  t2 <-
    redactedSearchAndInsert
      [ ("searchstr", "thriller"),
        ("artistname", "michael jackson"),
        -- ("year", "1982"),
        -- ("format", "MP3"),
        -- ("releasetype", "album"),
        ("order_by", "year")
      ]
  pure (t1 >> t2 >> t3)

redactedSearchAndInsert ::
  ( MonadLogger m1,
    MonadIO m1,
    MonadThrow m1,
    MonadPostgres m2,
    MonadThrow m2
  ) =>
  [(ByteString, ByteString)] ->
  m1 (Transaction m2 ())
redactedSearchAndInsert x =
  redactedSearch
    x
    ( do
        status <- Json.key "status" Json.asText
        when (status /= "success") $ do
          Json.throwCustomError [fmt|Status was not "success", but {status}|]
        Json.key "response" $ do
          Json.key "results" $
            sequence_
              <$> ( Json.eachInArray $ do
                      groupId <- Json.key "groupId" (Json.asIntegral @_ @Int)
                      groupName <- Json.key "groupName" Json.asText
                      fullJsonResult <-
                        Json.asObject
                          -- remove torrents cause they are inserted separately below
                          <&> KeyMap.filterWithKey (\k _ -> k /= "torrents")
                          <&> Json.Object
                      let insertTourGroup = do
                            _ <-
                              execute
                                [fmt|
                                  DELETE FROM redacted.torrent_groups
                                  WHERE group_id = ?::integer
                              |]
                                (Only groupId)
                            executeManyReturningWith
                              [fmt|
                                INSERT INTO redacted.torrent_groups (
                                  group_id, group_name, full_json_result
                                ) VALUES
                                ( ?, ? , ? )
                                RETURNING (id)
                              |]
                              [ ( groupId,
                                  groupName,
                                  fullJsonResult
                                )
                              ]
                              (label @"tourGroupIdPg" <$> Dec.fromField @Int)
                              >>= ensureSingleRow
                      insertTorrents <- Json.key "torrents" $ do
                        torrents <- Json.eachInArray $ do
                          torrentId <- Json.keyLabel @"torrentId" "torrentId" (Json.asIntegral @_ @Int)
                          fullJsonResultT <- label @"fullJsonResult" <$> Json.asValue
                          pure $ T2 torrentId fullJsonResultT
                        pure $ \dat -> do
                          _ <-
                            execute
                              [sql|
                                  DELETE FROM redacted.torrents_json
                                  WHERE torrent_id = ANY (?::integer[])
                            |]
                              (Only $ torrents & unzipT2 & (.torrentId) & PGArray)
                          execute
                            [sql|
                                  INSERT INTO redacted.torrents_json
                                        (torrent_id, torrent_group, full_json_result)
                                  SELECT inputs.torrent_id, static.torrent_group, inputs.full_json_result FROM
                                  (SELECT * FROM UNNEST(?::integer[], ?::jsonb[])) AS inputs(torrent_id, full_json_result)
                                  CROSS JOIN (VALUES(?::integer)) as static(torrent_group)
                            |]
                            ( torrents
                                & unzipT2
                                & \t ->
                                  ( t.torrentId & PGArray,
                                    t.fullJsonResult & PGArray,
                                    dat.tourGroupIdPg
                                  )
                            )
                          pure ()
                      pure (insertTourGroup >>= insertTorrents)
                  )
    )

migrate :: MonadPostgres m => Transaction m (Label "numberOfRowsAffected" Natural)
migrate = do
  execute_
    [sql|
    CREATE SCHEMA IF NOT EXISTS redacted;

    CREATE TABLE IF NOT EXISTS redacted.torrent_groups (
      id SERIAL PRIMARY KEY,
      group_id INTEGER,
      group_name TEXT,
      full_json_result JSONB,
      UNIQUE(group_id)
    );

    CREATE TABLE IF NOT EXISTS redacted.torrents_json (
      id SERIAL PRIMARY KEY,
      torrent_id INTEGER,
      torrent_group SERIAL NOT NULL REFERENCES redacted.torrent_groups(id) ON DELETE CASCADE,
      full_json_result JSONB,
      UNIQUE(torrent_id)
    );

    -- inflect out values of the full json

    CREATE OR REPLACE VIEW redacted.torrents AS
    SELECT
      t.id,
      t.torrent_id,
      t.torrent_group,
      -- the seeding weight is used to find the best torrent in a group.
      ( (full_json_result->'seeders')::integer*3
      + (full_json_result->'snatches')::integer)
      AS seeding_weight,
      t.full_json_result
    FROM redacted.torrents_json t;

    CREATE INDEX IF NOT EXISTS torrents_json_seeding ON redacted.torrents_json(((full_json_result->'seeding')::integer));
    CREATE INDEX IF NOT EXISTS torrents_json_snatches ON redacted.torrents_json(((full_json_result->'snatches')::integer));
  |]

data TorrentData = TorrentData
  { groupId :: Int,
    torrentId :: Int,
    torrentIdDb :: Int,
    seedingWeight :: Int,
    torrentJson :: Json.Value,
    torrentGroupJson :: T2 "artist" Text "groupName" Text
  }

getTorrentById :: (MonadPostgres m, HasField "id" r Int, MonadThrow m) => r -> Transaction m Json.Value
getTorrentById dat = do
  queryWith
    [sql|
    SELECT full_json_result FROM redacted.torrents
    WHERE id = ?::integer
  |]
    (getLabel @"id" dat)
    (Dec.json Json.asValue)
    >>= ensureSingleRow

-- | Find the best torrent for each torrent group (based on the seeding_weight)
getBestTorrents :: MonadPostgres m => Transaction m [TorrentData]
getBestTorrents = do
  queryWith
    [sql|
      SELECT * FROM (
        SELECT DISTINCT ON (group_id)
          tg.group_id,
          t.id,
          t.torrent_id,
          seeding_weight,
          t.full_json_result AS torrent_json,
          tg.full_json_result AS torrent_group_json
        FROM redacted.torrents t
        JOIN redacted.torrent_groups tg ON tg.id = t.torrent_group
        ORDER BY group_id, seeding_weight DESC
      ) as _
      ORDER BY seeding_weight DESC
    |]
    ()
    ( do
        groupId <- Dec.fromField @Int
        torrentIdDb <- Dec.fromField @Int
        torrentId <- Dec.fromField @Int
        seedingWeight <- Dec.fromField @Int
        torrentJson <- Dec.json Json.asValue
        torrentGroupJson <-
          ( Dec.json $ do
              artist <- Json.keyLabel @"artist" "artist" Json.asText
              groupName <- Json.keyLabel @"groupName" "groupName" Json.asText
              pure $ T2 artist groupName
            )
        pure $ TorrentData {..}
    )

hush :: Either a1 a2 -> Maybe a2
hush (Left _) = Nothing
hush (Right a) = Just a

unzipT2 :: forall l1 t1 l2 t2. [T2 l1 t1 l2 t2] -> T2 l1 [t1] l2 [t2]
unzipT2 xs = xs <&> toTup & unzip & fromTup
  where
    toTup :: forall a b. T2 a t1 b t2 -> (t1, t2)
    toTup (T2 a b) = (getField @a a, getField @b b)
    fromTup :: (a, b) -> T2 l1 a l2 b
    fromTup (t1, t2) = T2 (label @l1 t1) (label @l2 t2)

unzipT3 :: forall l1 t1 l2 t2 l3 t3. [T3 l1 t1 l2 t2 l3 t3] -> T3 l1 [t1] l2 [t2] l3 [t3]
unzipT3 xs = xs <&> toTup & unzip3 & fromTup
  where
    toTup :: forall a b c. T3 a t1 b t2 c t3 -> (t1, t2, t3)
    toTup (T3 a b c) = (getField @a a, getField @b b, getField @c c)
    fromTup :: (a, b, c) -> T3 l1 a l2 b l3 c
    fromTup (t1, t2, t3) = T3 (label @l1 t1) (label @l2 t2) (label @l3 t3)

redactedApiRequest ::
  ( MonadThrow m,
    MonadIO m,
    MonadLogger m,
    HasField "action" p ByteString,
    HasField "actionArgs" p [(ByteString, Maybe ByteString)]
  ) =>
  p ->
  Json.Parse ErrorTree a ->
  m a
redactedApiRequest dat parse = do
  authKey <- runCommandExpect0 "pass" ["internet/redacted/api-keys/whatcd-resolver"]
  let req =
        [fmt|https://redacted.ch/ajax.php|]
          & Http.setRequestMethod "GET"
          & Http.setQueryString (("action", Just dat.action) : dat.actionArgs)
          & Http.setRequestHeader "Authorization" [authKey]
  Http.httpBS req
    >>= assertM
      ( \resp -> case resp & Http.responseStatus & (.statusCode) of
          200 -> Right $ resp & Http.responseBody
          _ -> Left [fmt|Redacted returned an non-200 error code: {resp & showPretty}|]
      )
    >>= ( Json.parseStrict parse
            >>> first (Json.parseErrorTree "could not parse redacted response")
            >>> assertM id
        )

assertM :: MonadThrow f => (t -> Either ErrorTree a) -> t -> f a
assertM f v = case f v of
  Right a -> pure a
  Left err -> appThrowTree err

runAppWith :: AppT IO a -> IO (Either TmpPg.StartError a)
runAppWith appT = withDb $ \db -> do
  tools <- initMonadTools (label @"envvar" "WHATCD_RESOLVER_TOOLS")
  let config = label @"logDatabaseQueries" LogDatabaseQueries
  pgConnPool <-
    Pool.createPool
      (Postgres.connectPostgreSQL (db & TmpPg.toConnectionString))
      Postgres.close
      {- number of stripes -} 5
      {- unusedResourceOpenTime -} 10
      {- max resources per stripe -} 10
  transmissionSessionId <- newEmptyMVar
  let newAppT = do
        logInfo [fmt|Running with config: {showPretty config}|]
        logInfo [fmt|Connected to database at {db & TmpPg.toDataDirectory} on socket {db & TmpPg.toConnectionString}|]
        appT
  runReaderT newAppT.unAppT Context {..}

withDb :: (TmpPg.DB -> IO a) -> IO (Either TmpPg.StartError a)
withDb act = do
  dataDir <- Xdg.getXdgDirectory Xdg.XdgData "whatcd-resolver"
  let databaseDir = dataDir </> "database"
  let socketDir = dataDir </> "database-socket"
  Dir.createDirectoryIfMissing True socketDir
  initDbConfig <-
    Dir.doesDirectoryExist databaseDir >>= \case
      True -> pure TmpPg.Zlich
      False -> do
        putStderrLn [fmt|Database does not exist yet, creating in "{databaseDir}"|]
        Dir.createDirectoryIfMissing True databaseDir
        pure TmpPg.DontCare
  let cfg =
        mempty
          { TmpPg.dataDirectory = TmpPg.Permanent (databaseDir),
            TmpPg.socketDirectory = TmpPg.Permanent socketDir,
            TmpPg.port = pure $ Just 5432,
            TmpPg.initDbConfig
          }
  TmpPg.withConfig cfg $ \db -> do
    -- print [fmt|data dir: {db & TmpPg.toDataDirectory}|]
    -- print [fmt|conn string: {db & TmpPg.toConnectionString}|]
    act db

data Context = Context
  { config :: Label "logDatabaseQueries" DatabaseLogging,
    tools :: Tools,
    pgConnPool :: Pool Postgres.Connection,
    transmissionSessionId :: MVar ByteString
  }

newtype AppT m a = AppT {unAppT :: ReaderT Context m a}
  deriving newtype (Functor, Applicative, Monad, MonadIO, MonadUnliftIO, MonadThrow)

type App a = AppT IO a

data AppException = AppException Text
  deriving stock (Show)
  deriving anyclass (Exception)

appThrowTree :: MonadThrow m => ErrorTree -> m a
appThrowTree exc = throwM $ AppException $ prettyErrorTree exc

instance MonadIO m => MonadLogger (AppT m) where
  monadLoggerLog loc src lvl msg = liftIO $ Logger.defaultOutput IO.stderr loc src lvl (Logger.toLogStr msg)

instance Monad m => MonadTools (AppT m) where
  getTools = AppT $ asks (.tools)

class MonadTransmission m where
  getTransmissionId :: m (Maybe ByteString)
  setTransmissionId :: ByteString -> m ()

instance (MonadIO m) => MonadTransmission (AppT m) where
  getTransmissionId = AppT (asks (.transmissionSessionId)) >>= tryTakeMVar
  setTransmissionId t = do
    var <- AppT $ asks (.transmissionSessionId)
    putMVar var t

instance (MonadThrow m, MonadUnliftIO m) => MonadPostgres (AppT m) where
  execute qry params = do
    conf <- lift $ AppT (asks (.config))
    logQueryIfEnabled conf qry (HasSingleParam params)
    pgExecute qry params
  execute_ qry = do
    conf <- lift $ AppT (asks (.config))
    logQueryIfEnabled @(Only Text) conf qry HasNoParams
    pgExecute_ qry
  executeMany qry params = do
    conf <- lift $ AppT (asks (.config))
    logQueryIfEnabled conf qry (HasMultiParams params)
    pgExecuteMany qry params
  executeManyReturningWith qry params dec = do
    conf <- lift $ AppT (asks (.config))
    logQueryIfEnabled conf qry (HasMultiParams params)
    pgExecuteManyReturningWith qry params dec

  queryWith qry params decoder = do
    conf <- lift $ AppT (asks (.config))
    logQueryIfEnabled conf qry (HasSingleParam params)
    pgQueryWith qry params decoder

  -- TODO: log these queries as well with `logQueryIfEnabled`, but test out whether it works with query_ and foldRows first.
  queryWith_ = pgQueryWith_
  foldRows = pgFold

  runTransaction = runPGTransaction

runPGTransaction :: MonadUnliftIO m => Transaction (AppT m) a -> AppT m a
runPGTransaction (Transaction transaction) = do
  pool <- AppT ask <&> (.pgConnPool)
  withRunInIO $ \unliftIO ->
    withPGTransaction pool $ \conn -> do
      unliftIO $ runReaderT transaction conn

-- | Perform a Postgres action within a transaction
withPGTransaction ::
  -- | Postgres connection pool to be used for the action
  Pool Postgres.Connection ->
  -- | DB-action to be performed
  (Postgres.Connection -> IO a) ->
  -- | Result of the DB-action
  IO a
withPGTransaction connPool f =
  Pool.withResource
    connPool
    (\conn -> Postgres.withTransaction conn (f conn))

data HasQueryParams param
  = HasNoParams
  | HasSingleParam param
  | HasMultiParams [param]

-- | Log the postgres query depending on the setting of @config.debugInfo.logDatabaseQueries@.
logQueryIfEnabled ::
  forall params config m.
  ( Postgres.ToRow params,
    MonadUnliftIO m,
    MonadLogger m,
    MonadTools m,
    HasField "logDatabaseQueries" config DatabaseLogging
  ) =>
  config ->
  Postgres.Query ->
  HasQueryParams params ->
  Transaction m ()
logQueryIfEnabled config qry params = do
  -- In case we have query logging enabled, we want to do that
  let formattedQuery = case params of
        HasNoParams -> pgFormatQueryNoParams' qry
        HasSingleParam p -> pgFormatQuery' qry p
        HasMultiParams ps -> pgFormatQueryMany' qry ps

  let doLog errs =
        errs
          & nestedMultiError "Postgres query"
          & prettyErrorTree
          & logDebug
          & lift
  let addQuery = do
        formattedQuery
          <&> newError
          <&> singleError
  let addExplain = do
        q <- formattedQuery
        pgQueryWith_
          ( "EXPLAIN "
              <> (
                   -- TODO: this is not nice, but the only way to get the `executeMany` form to work with this
                   -- because we need the query with all elements already interpolated.
                   Postgres.Query (q & textToBytesUtf8)
                 )
          )
          (Dec.fromField @Text)
          <&> Text.intercalate "\n"
          <&> newError
          <&> singleError

  case config.logDatabaseQueries of
    DontLogDatabaseQueries -> pure ()
    LogDatabaseQueries -> do
      aq <- addQuery
      doLog (aq :| [])
    LogDatabaseQueriesAndExplain -> do
      aq <- addQuery
      -- XXX: stuff like `CREATE SCHEMA` cannot be EXPLAINed, so we should catch exceptions here
      -- and just ignore anything that errors (if it errors because of a problem with the query, it would have been caught by the query itself.
      ex <- addExplain
      doLog (nestedError "Query" aq :| [nestedError "Explain" ex])

data DatabaseLogging
  = DontLogDatabaseQueries
  | LogDatabaseQueries
  | LogDatabaseQueriesAndExplain
  deriving stock (Show)