import { Association, Attributes, CreationAttributes, CreationOptional, DataTypes, HasManyGetAssociationsMixin, HasOneCreateAssociationMixin, HasOneGetAssociationMixin, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core";
import { Post } from "./Post";
import { UserPerms } from "./UserPerms";
import { Auth } from "./Auth";
import {
    PrimaryKey,
    Attribute,
    AutoIncrement,
    NotNull,
    BelongsTo,
    Unique,
    HasMany,
    HasOne,
    UpdatedAt,
    CreatedAt,
} from '@sequelize/core/decorators-legacy';

import { SqliteDialect } from '@sequelize/sqlite3';

type UserCreationAttributes = {
    username:string;
    password:string;
    perms?:Partial<InferAttributes<UserPerms>>
}
export class User extends Model<InferAttributes<User>, InferCreationAttributes<User>>{
    @Attribute(DataTypes.INTEGER)
    @PrimaryKey
    @AutoIncrement
    @Unique
    declare id: CreationOptional<number>;
    @Attribute(DataTypes.STRING)
    declare username: string;
    @Attribute(DataTypes.STRING)
    declare password: string;

    // Date thingies
    
    @CreatedAt
    declare createdAt: CreationOptional<Date>;
    @UpdatedAt
    declare updatedAt: CreationOptional<Date>;

    // Associations
    declare getAuthTokens:HasManyGetAssociationsMixin<Auth>;
    declare getPosts:HasManyGetAssociationsMixin<Post>;
    declare getPerms:HasOneGetAssociationMixin<UserPerms>;
    declare createPerms:HasOneCreateAssociationMixin<UserPerms>;

    /** Defined by {@link Auth.user} */
    declare authtokens?:NonAttribute<Auth[]>
    /** Defined by {@link UserPerms.user} */
    declare perms?:CreationOptional<CreationAttributes<UserPerms>>
    /** Defined by {@link Post.user} */
    declare posts?:CreationOptional<Post[]>


    declare static associations: {
        perms:      Association<UserPerms,User>;
        posts:      Association<Post,User>
        authtokens: Association<Auth,User>;
    };
    
}
export function addUserScopes(){
    User.addScope('defaultScope',{
        attributes: {
            exclude: ['password', 'createdAt', 'updatedAt'],
        }
    });
    User.addScope('withPerms',{
        include: [{association: 'perms' }]
    })
    User.addScope('withAuthtokens',{
        include: [{association: 'authtokens'}]
    })
}