import {
  CreationOptional,
  DataTypes,
  InferAttributes,
  InferCreationAttributes,
  Model,
  NonAttribute,
  Sequelize,
} from "sequelize";
import { InvoiceParticipant_Model } from "./invoiceParticipant.mo.del";

export type TCreationInvoiceParticipantAddress_Model = InferCreationAttributes<
  InvoiceParticipantAddress_Model,
  { omit: "participant" }
>;

export class InvoiceParticipantAddress_Model extends Model<
  InferAttributes<InvoiceParticipantAddress_Model, { omit: "participant" }>,
  InferCreationAttributes<InvoiceParticipantAddress_Model, { omit: "participant" }>
> {
  static associate(connection: Sequelize) {
    const { InvoiceParticipantAddress_Model, InvoiceParticipant_Model } = connection.models;
    InvoiceParticipantAddress_Model.belongsTo(InvoiceParticipant_Model, {
      as: "participant",
      foreignKey: "participant_id",
    });
  }

  declare address_id: string;
  declare participant_id: string;
  declare type: string;
  declare street: CreationOptional<string>;
  declare postal_code: CreationOptional<string>;
  declare city: CreationOptional<string>;
  declare province: CreationOptional<string>;
  declare country: CreationOptional<string>;
  declare phone: CreationOptional<string>;
  declare email: CreationOptional<string>;

  declare participant?: NonAttribute<InvoiceParticipant_Model>;
}

export default (sequelize: Sequelize) => {
  InvoiceParticipantAddress_Model.init(
    {
      address_id: {
        type: new DataTypes.UUID(),
        primaryKey: true,
      },
      participant_id: {
        type: new DataTypes.UUID(),
        primaryKey: true,
      },
      type: {
        type: new DataTypes.STRING(),
        allowNull: false,
      },
      street: {
        type: new DataTypes.STRING(),
        allowNull: true,
      },
      postal_code: {
        type: new DataTypes.STRING(),
        allowNull: true,
      },
      city: {
        type: new DataTypes.STRING(),
        allowNull: true,
      },
      province: {
        type: new DataTypes.STRING(),
        allowNull: true,
      },
      country: {
        type: new DataTypes.STRING(),
        allowNull: true,
      },
      email: {
        type: new DataTypes.STRING(),
        allowNull: true,
      },
      phone: {
        type: new DataTypes.STRING(),
        allowNull: true,
      },
    },
    {
      sequelize,
      tableName: "invoice_participant_addresses",
    }
  );

  return InvoiceParticipantAddress_Model;
};
