package models

import (
	"brainminder.speedtech.it/internal/database"
	"database/sql"
	"errors"
)

type ItemShareModel struct {
	*BaseModel
}

type ItemShare struct {
	Id                      int64  `db:"id"`
	Token                   string `db:"token"`
	ItemId                  int64  `db:"item_id"`
	PermissionEdit          int    `db:"permission_edit"`
	StartDatetime           string `db:"start_datetime"`
	EndDatetime             string `db:"end_datetime"`
	Password                string `db:"password"`
	ItemTitle               string `db:"item_title"`
	ItemSummaryRendered     string `db:"item_summary_rendered"`
	ItemDescriptionRendered string `db:"item_description_rendered"`
}

func (model *ItemShareModel) One(id int64) (*ItemShare, bool, error) {
	ctx, cancel := database.GetContext()
	defer cancel()

	var row ItemShare

	query := `SELECT bmi.title AS item_title, bmi.summary_rendered as item_summary_rendered, bmi.description_rendered as item_description_rendered, 
       bmis.* FROM bm_item_shares bmis 
	INNER JOIN bm_item bmi ON bmis.item_id=bmi.id WHERE bmis.id = $1`

	err := model.DB.GetContext(ctx, &row, query, id)
	if errors.Is(err, sql.ErrNoRows) {
		return nil, false, nil
	}

	return &row, true, err
}

func (model *ItemShareModel) OneByToken(token string) (*ItemShare, bool, error) {
	ctx, cancel := database.GetContext()
	defer cancel()

	var row ItemShare

	query := `SELECT bmi.title AS item_title, bmi.summary_rendered as item_summary_rendered, bmi.description_rendered as item_description_rendered,  
       bmis.* FROM bm_item_shares bmis 
	INNER JOIN bm_item bmi ON bmis.item_id=bmi.id WHERE bmis.token = $1`

	err := model.DB.GetContext(ctx, &row, query, token)
	if errors.Is(err, sql.ErrNoRows) {
		return nil, false, nil
	}

	return &row, true, err
}

func (model *ItemShareModel) Create(ItemShare *ItemShare) (int64, error) {
	ctx, cancel := database.GetContext()
	defer cancel()

	query := `INSERT INTO bm_item_shares (token, item_id, permission_edit, start_datetime, end_datetime, password) VALUES (:token, :item_id, :permission_edit, :start_datetime, :end_datetime, :password)`

	result, err := model.DB.NamedExecContext(ctx, query, ItemShare)
	if err != nil {
		return 0, err
	}

	id, err := result.LastInsertId()
	if err != nil {
		return 0, err
	}

	return id, err
}

func (model *ItemShareModel) Update(ItemShare *ItemShare) error {
	ctx, cancel := database.GetContext()
	defer cancel()

	query := `UPDATE bm_item_shares SET token=:token, permission_edit=:permission_edit, start_datetime=:start_datetime, end_datetime=:end_datetime, password=:password WHERE id = :id`
	_, err := model.DB.NamedExecContext(ctx, query, ItemShare)

	if err != nil {
		return err
	}

	return err
}

func (model *ItemShareModel) Delete(id int) (bool, error) {
	ctx, cancel := database.GetContext()
	defer cancel()

	_, err := model.DB.ExecContext(ctx, `DELETE FROM bm_item_shares WHERE id = $1`, id)
	if errors.Is(err, sql.ErrNoRows) {
		return false, nil
	}

	return true, err
}
