package controller

import (
	"errors"
	"fmt"

	"arimelody-web/model"
	"github.com/jmoiron/sqlx"
)

func GetRelease(db *sqlx.DB, id string, full bool) (*model.Release, error) {
    var release = model.Release{}

    err := db.Get(&release, "SELECT * FROM musicrelease WHERE id=$1", id)
    if err != nil {
        return nil, err
    }

    if full {
        // get credits
        credits, err := GetReleaseCredits(db, id)
        if err != nil {
            return nil, errors.New(fmt.Sprintf("Credits: %s", err))
        }
        for _, credit := range credits {
            release.Credits = append(release.Credits, credit)
        }

        // get tracks
        tracks, err := GetReleaseTracks(db, id)
        if err != nil {
            return nil, errors.New(fmt.Sprintf("Tracks: %s", err))
        }
        for _, track := range tracks {
            release.Tracks = append(release.Tracks, track)
        }

        // get links
        links, err := GetReleaseLinks(db, id)
        if err != nil {
            return nil, errors.New(fmt.Sprintf("Links: %s", err))
        }
        for _, link := range links {
            release.Links = append(release.Links, link)
        }
    }

    return &release, nil
}

func GetAllReleases(db *sqlx.DB, onlyVisible bool, limit int, full bool) ([]*model.Release, error) {
    var releases = []*model.Release{}

    query := "SELECT * FROM musicrelease"
    if onlyVisible {
        query += " WHERE visible=true"
    }
    query += " ORDER BY release_date DESC"
    var err error
    if limit > 0 {
        err = db.Select(&releases, query + " LIMIT $1", limit)
    } else {
        err = db.Select(&releases, query)
    }
    if err != nil {
        return nil, err
    }

    for _, release := range releases {
        // get credits
        credits, err := GetReleaseCredits(db, release.ID)
        if err != nil {
            return nil, errors.New(fmt.Sprintf("Credits: %s", err))
        }
        for _, credit := range credits {
            release.Credits = append(release.Credits, credit)
        }

        if full {
            // get tracks
            tracks, err := GetReleaseTracks(db, release.ID)
            if err != nil {
                return nil, errors.New(fmt.Sprintf("Tracks: %s", err))
            }
            for _, track := range tracks {
                release.Tracks = append(release.Tracks, track)
            }

            // get links
            links, err := GetReleaseLinks(db, release.ID)
            if err != nil {
                return nil, errors.New(fmt.Sprintf("Links: %s", err))
            }
            for _, link := range links {
                release.Links = append(release.Links, link)
            }
        }
    }

    return releases, nil
}

func CreateRelease(db *sqlx.DB, release *model.Release) error {
    _, err := db.Exec(
        "INSERT INTO musicrelease "+
        "(id, visible, title, description, type, release_date, artwork, buyname, buylink, copyright, copyrighturl) "+
        "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
        release.ID,
        release.Visible,
        release.Title,
        release.Description,
        release.ReleaseType,
        release.ReleaseDate.Format("2006-01-02 15:04:05"),
        release.Artwork,
        release.Buyname,
        release.Buylink,
        release.Copyright,
        release.CopyrightURL,
    )
    if err != nil {
        return err
    }

    return nil
}

func UpdateRelease(db *sqlx.DB, release *model.Release) error {
    _, err := db.Exec(
        "UPDATE musicrelease SET "+
        "visible=$2, title=$3, description=$4, type=$5, release_date=$6, artwork=$7, buyname=$8, buylink=$9, copyright=$10, copyrighturl=$11 "+
        "WHERE id=$1",
        release.ID,
        release.Visible,
        release.Title,
        release.Description,
        release.ReleaseType,
        release.ReleaseDate.Format("2006-01-02 15:04:05"),
        release.Artwork,
        release.Buyname,
        release.Buylink,
        release.Copyright,
        release.CopyrightURL,
    )
    if err != nil {
        return err
    }

    return nil
}

func UpdateReleaseTracks(db *sqlx.DB, releaseID string, new_tracks []string) error {
    tx, err := db.Begin()
    if err != nil {
        return err
    }

    _, err = tx.Exec("DELETE FROM musicreleasetrack WHERE release=$1", releaseID)
    if err != nil {
        return err
    }
    for i, trackID := range new_tracks {
        _, err = tx.Exec(
            "INSERT INTO musicreleasetrack "+
            "(release, track, number) "+
            "VALUES ($1, $2, $3)",
            releaseID,
            trackID,
            i)
        if err != nil {
            return err
        }
    }

    err = tx.Commit()
    if err != nil {
        return err
    }

    return nil
}

func UpdateReleaseCredits(db *sqlx.DB, releaseID string, new_credits []*model.Credit) error {
    tx, err := db.Begin()
    if err != nil {
        return err
    }

    _, err = tx.Exec("DELETE FROM musiccredit WHERE release=$1", releaseID)
    if err != nil {
        return err
    }
    for _, credit := range new_credits {
        _, err = tx.Exec(
            "INSERT INTO musiccredit "+
            "(release, artist, role, is_primary) "+
            "VALUES ($1, $2, $3, $4)",
            releaseID,
            credit.Artist.ID,
            credit.Role,
            credit.Primary,
        )
        if err != nil {
            return err
        }
    }

    err = tx.Commit()
    if err != nil {
        return err
    }

    return nil
}

func UpdateReleaseLinks(db *sqlx.DB, releaseID string, new_links []*model.Link) error {
    tx, err := db.Begin()
    if err != nil {
        return err
    }

    _, err = tx.Exec("DELETE FROM musiclink WHERE release=$1", releaseID)
    if err != nil {
        return err
    }
    for _, link := range new_links {
        fmt.Printf("%s: %s\n", link.Name, link.URL)
        _, err := tx.Exec(
            "INSERT INTO musiclink "+
            "(release, name, url) "+
            "VALUES ($1, $2, $3)",
            releaseID,
            link.Name,
            link.URL,
        )
        if err != nil {
            return err
        }
    }

    err = tx.Commit()
    if err != nil {
        return err
    }

    return nil
}

func DeleteRelease(db *sqlx.DB, releaseID string) error {
    _, err := db.Exec(
        "DELETE FROM musicrelease "+
        "WHERE id=$1",
        releaseID,
    )
    if err != nil {
        return err
    }

    return nil
}

func GetReleaseTracks(db *sqlx.DB, releaseID string) ([]*model.Track, error) {
    var tracks = []*model.Track{}

    err := db.Select(&tracks,
        "SELECT musictrack.* FROM musictrack "+
        "JOIN musicreleasetrack ON track=id "+
        "WHERE release=$1 "+
        "ORDER BY number ASC",
        releaseID,
    )
    if err != nil {
        return nil, err
    }

    return tracks, nil
}

func GetReleaseCredits(db *sqlx.DB, releaseID string) ([]*model.Credit, error) {
    rows, err := db.Query(
        "SELECT artist.id,artist.name,artist.website,artist.avatar,role,is_primary "+
        "FROM musiccredit "+
        "JOIN artist ON artist=artist.id "+
        "JOIN musicrelease ON release=musicrelease.id "+
        "WHERE musicrelease.id=$1 "+
        "ORDER BY is_primary DESC",
        releaseID,
    )
    if err != nil {
        return nil, err
    }

    var credits []*model.Credit
    for rows.Next() {
        credit := model.Credit{}
        rows.Scan(
            &credit.Artist.ID,
            &credit.Artist.Name,
            &credit.Artist.Website,
            &credit.Artist.Avatar,
            &credit.Role,
            &credit.Primary)
        credits = append(credits, &credit)
    }

    return credits, nil
}

func GetReleaseLinks(db *sqlx.DB, releaseID string) ([]*model.Link, error) {
    var links = []*model.Link{}

    err := db.Select(&links, "SELECT name,url FROM musiclink WHERE release=$1", releaseID)
    if err != nil {
        return nil, err
    }

    return links, nil
}
