<?php
namespace app\models;

use app\app;
use app\models\addresses;
use swentel\nostr\Key\Key;

class users
{
    public static function init()
    {
        app::$db->exec("CREATE TABLE IF NOT EXISTS users (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            email TEXT UNIQUE,
            shipping_address_id INTEGER,
            billing_address_id INTEGER,
            opt_in_promotional BOOLEAN NOT NULL,
            opt_in_subscription BOOLEAN DEFAULT TRUE,
            opt_in_orders BOOLEAN DEFAULT TRUE,
            lifetime_spend INTEGER DEFAULT 0,
            lifetime_orders INTEGER DEFAULT 0,
            verified BOOLEAN NOT NULL,
            dark_theme BOOLEAN NOT NULL,
            nsec TEXT,
            npub TEXT NOT NULL,
            attached_lightning_address TEXT,
            replace_email_token TEXT,
            name TEXT,
            company_name TEXT,
            company_type TEXT,
            company_size TEXT,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP
        )");
    }

    public static function updateReplaceEmailTokenById($user_id, $replace_token)
    {
        $query = "UPDATE users SET replace_email_token = :replace_token WHERE id = :user_id";
        $stmt  = app::$db->prepare($query);
        $stmt->bindParam(':replace_token', $replace_token);
        $stmt->bindParam(':user_id', $user_id);
        $stmt->execute();
    }

    public static function updateEmailById($user_id, $email)
    {
        $query = "UPDATE users SET email = :email WHERE id = :user_id";
        $stmt  = app::$db->prepare($query);
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':user_id', $user_id);
        $stmt->execute();
        users::updateReplaceEmailTokenById($user_id, null);
    }

    public static function getByReplaceEmailToken($token)
    {
        $query = "SELECT * FROM users WHERE replace_email_token = :token";
        $stmt  = app::$db->prepare($query);
        $stmt->bindParam(':token', $token);
        $stmt->execute();
        return $stmt->fetch(\PDO::FETCH_ASSOC);
    }

    public static function updateProfileById($user_id, $post)
    {
        $query = "UPDATE users SET
            name = :name,
            company_name = :company_name,
            company_type = :company_type,
            company_size = :company_size
            WHERE id = :user_id";
        $stmt = app::$db->prepare($query);
        $stmt->bindParam(':name', $post['name']);
        $stmt->bindParam(':company_name', $post['company_name']);
        $stmt->bindParam(':company_type', $post['company_type']);
        $stmt->bindParam(':company_size', $post['company_size']);
        $stmt->bindParam(':user_id', $user_id);
        $stmt->execute();
    }

    public static function add($email, $ship_id, $bill_id, $opt_in_promotional, $verified, $dark_theme)
    {
        $key         = new Key();
        $private_key = $key->generatePrivateKey();
        $public_key  = $key->getPublicKey($private_key);
        $npub        = $key->convertPublicKeyToBech32($public_key);
        $nsec        = $key->convertPrivateKeyToBech32($private_key);
        $query       = "INSERT INTO users (
            email,
            shipping_address_id,
            billing_address_id,
            opt_in_promotional,
            verified,
            dark_theme,
            nsec,
            npub
        ) VALUES (
            :email,
            :shipping_address_id,
            :billing_address_id,
            :opt_in_promotional,
            :verified,
            :dark_theme,
            :nsec,
            :npub
        )";
        $stmt = app::$db->prepare($query);
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':shipping_address_id', $ship_id);
        $stmt->bindParam(':billing_address_id', $bill_id);
        $stmt->bindParam(':opt_in_promotional', $opt_in_promotional);
        $stmt->bindParam(':verified', $verified);
        $stmt->bindParam(':dark_theme', $dark_theme);
        $stmt->bindParam(':nsec', $nsec);
        $stmt->bindParam(':npub', $npub);
        $stmt->execute();
        $user_id = app::$db->lastInsertId();
        addresses::updateUserIdById($ship_id, $user_id);
        if ($ship_id != $bill_id) {
            addresses::updateUserIdById($bill_id, $user_id);
        }
        return $user_id;
    }

    public static function verify($email)
    {
        $query = "UPDATE users SET verified = 1 WHERE email = :email";
        $stmt  = app::$db->prepare($query);
        $stmt->bindParam(':email', $email);
        $stmt->execute();
    }

    public static function getById($id)
    {
        $query = "SELECT * FROM users WHERE id = :id";
        $stmt  = app::$db->prepare($query);
        $stmt->bindParam(':id', $id);
        $stmt->execute();
        return $stmt->fetch(\PDO::FETCH_ASSOC);
    }

    public static function getByNpub($npub)
    {
        $query = "SELECT * FROM users WHERE npub = :npub";
        $stmt  = app::$db->prepare($query);
        $stmt->bindParam(':npub', $npub);
        $stmt->execute();
        return $stmt->fetch(\PDO::FETCH_ASSOC);
    }

    public static function getByEmail($email)
    {
        $query = "SELECT * FROM users WHERE email = :email";
        $stmt  = app::$db->prepare($query);
        $stmt->bindParam(':email', $email);
        $stmt->execute();
        return $stmt->fetch(\PDO::FETCH_ASSOC);
    }
}
