import {
  CreationOptional,
  DataTypes,
  ForeignKey,
  InferAttributes,
  InferCreationAttributes,
  Model,
  NonAttribute,
  Sequelize,
} from "sequelize";
import { Contact_Model } from "./contact.mo.del.ts.bak";

export type TCreationContactAddress_Attributes = InferCreationAttributes<
  ContactAddress_Model,
  { omit: "customer" }
>;

export class ContactAddress_Model extends Model<
  InferAttributes<ContactAddress_Model, { omit: "customer" }>,
  TCreationContactAddress_Attributes
> {
  // To avoid table creation
  static async sync(): Promise<any> {
    return Promise.resolve();
  }

  static associate(connection: Sequelize) {
    const { Contact_Model, ContactAddress_Model } = connection.models;

    ContactAddress_Model.belongsTo(Contact_Model, {
      as: "customer",
      foreignKey: "customer_id",
    });
  }

  declare id: string;
  declare customer_id: ForeignKey<Contact_Model["id"]>;
  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 customer?: NonAttribute<Contact_Model>;
}

export default (sequelize: Sequelize) => {
  ContactAddress_Model.init(
    {
      id: {
        type: DataTypes.UUID,
        primaryKey: true,
      },
      customer_id: new DataTypes.UUID(),
      type: DataTypes.STRING(),
      street: DataTypes.STRING(),
      postal_code: DataTypes.STRING(),
      city: DataTypes.STRING,
      province: DataTypes.STRING,
      country: DataTypes.STRING,
      email: DataTypes.STRING,
      phone: DataTypes.STRING,
    },
    {
      sequelize,
      tableName: "customer_addresses",
      timestamps: false,
    }
  );

  return ContactAddress_Model;
};
