//
//  tank
//
//  Import and Playlist Daemon for autoradio project
//
//
//  Copyright (C) 2017 Christian Pointner <equinox@helsinki.at>
//
//  This file is part of tank.
//
//  tank is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  any later version.
//
//  tank is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with tank. If not, see <http://www.gnu.org/licenses/>.
//

package store

import (
	"encoding/binary"
	"errors"
	"time"
)

const (
	// well-known bucket names
	infoBn   = "_info"
	groupsBn = "groups"

	publicGroupBn = "_public"

	filesBn     = "files"
	playlistsBn = "playlists"

	// well-known keys
	storeVersionKey = "Version"
)

var (
	groupBuckets = []string{filesBn, playlistsBn} // these buckets will be created inside every group bucket

	ErrNotImplented = errors.New("not implemented")
	ErrNotFound     = errors.New("not found")
)

func itob(v int) []byte {
	b := make([]byte, 8)
	binary.BigEndian.PutUint64(b, uint64(v))
	return b
}

func btoi(b []byte) int {
	return int(binary.BigEndian.Uint64(b))
}

type TimeAndUser struct {
	User      string    `json:"u"`
	Timestamp time.Time `josn:"ts"`
}

// stored in filesBn
type FileSource struct {
	FileName    string `json:"f"`
	ContentHash string `json:"h"`
}

type FileMetadata struct {
	Artist string `json:"a"`
	Title  string `json:"t"`
	Album  string `json:"b"`
	// TODO: add more fields
}

type File struct {
	Created     TimeAndUser  `json:"cr"`
	LastChanged TimeAndUser  `json:"ch"`
	Source      FileSource   `json:"src"`
	Metadata    FileMetadata `json:"m"`
	Size        uint64       `json:"s"`
	Used        bool         `json:"u"`
	Dirty       bool         `json:"d"`
}

type Files map[string]File

// stored in filesBn
type Playlist struct {
	Created     TimeAndUser `json:"cr"`
	LastChanged TimeAndUser `json:"ch"`
	// TODO: playlist storage layout
}

type Playlists map[string]Playlist