import {
  CreationOptional,
  DataTypes,
  InferAttributes,
  InferCreationAttributes,
  Model,
  NonAttribute,
  Sequelize,
} from "sequelize";
import { Quote_Model } from "./quote.model";

export type QuoteItemCreationAttributes = InferCreationAttributes<
  QuoteItem_Model,
  { omit: "quote" }
>;

export class QuoteItem_Model extends Model<
  InferAttributes<QuoteItem_Model, { omit: "quote" }>,
  InferCreationAttributes<QuoteItem_Model, { omit: "quote" }>
> {
  static associate(connection: Sequelize) {
    const { Quote_Model, QuoteItem_Model } = connection.models;

    QuoteItem_Model.belongsTo(Quote_Model, {
      as: "quote",
      foreignKey: "quote_id",
      onDelete: "CASCADE",
    });
  }

  declare quote_id: string;
  declare item_id: string;
  declare id_article: CreationOptional<number | null>;
  declare position: number;
  declare description: CreationOptional<string | null>;
  declare quantity: CreationOptional<number | null>;
  declare unit_price: CreationOptional<number | null>;
  declare subtotal_price: CreationOptional<number | null>;
  declare discount: CreationOptional<number | null>;
  declare total_price: CreationOptional<number | null>;

  declare quote: NonAttribute<Quote_Model>;
}

export default (sequelize: Sequelize) => {
  QuoteItem_Model.init(
    {
      item_id: {
        type: new DataTypes.UUID(),
        primaryKey: true,
      },
      quote_id: {
        type: new DataTypes.UUID(),
        primaryKey: true,
      },
      id_article: {
        type: DataTypes.BIGINT().UNSIGNED,
        allowNull: true,
      },
      position: {
        type: new DataTypes.MEDIUMINT(),
        autoIncrement: false,
        allowNull: false,
      },
      description: {
        type: new DataTypes.TEXT(),
        allowNull: true,
      },
      quantity: {
        type: DataTypes.BIGINT(),
        allowNull: true,
      },
      unit_price: {
        type: new DataTypes.BIGINT(),
        allowNull: true,
      },
      subtotal_price: {
        type: new DataTypes.BIGINT(),
        allowNull: true,
      },
      discount: {
        type: new DataTypes.SMALLINT(),
        allowNull: true,
      },
      total_price: {
        type: new DataTypes.BIGINT(),
        allowNull: true,
      },
    },
    {
      sequelize,
      tableName: "quote_items",
      timestamps: false,

      indexes: [],
    }
  );

  return QuoteItem_Model;
};
