const { DataTypes } = require("sequelize");
const sequelize = require("../db");

const Companies = sequelize.define("Companies", {
  name: {
    type: DataTypes.TEXT,
  },
  country: {
    type: DataTypes.ENUM,
    values: ["NO", "DK", "FI", "SE", "US", "GB", "AE"],
  },
  orgNumber: {
    type: DataTypes.STRING,
  },
  phoneNumber: {
    type: DataTypes.STRING,
  },
  address1: {
    type: DataTypes.STRING,
  },
  address2: {
    type: DataTypes.STRING,
  },
  city: {
    type: DataTypes.STRING,
  },
  zipCode: {
    type: DataTypes.STRING,
  },
  email: {
    type: DataTypes.STRING,
  },
  subscriptionEndsAt: {
    type: DataTypes.DATE,
  },
  subscriptionType: DataTypes.ENUM("free trial", "paid", "trial"),
  tenantId: {
    type: DataTypes.STRING,
    allowNull: true,
    comment: "Azure AD tenant ID (tid)",
  },
  msAccessToken: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: "Encrypted Microsoft Graph access token",
  },
  msRefreshToken: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: "Encrypted Microsoft refresh token",
  },
  msTokenExpiresAt: {
    type: DataTypes.DATE,
    allowNull: true,
  },
  msSharepointSiteId: {
    type: DataTypes.STRING,
    allowNull: true,
  },
  msConnected: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false,
  },
  spListMapping: {
    type: DataTypes.JSON,
    allowNull: true,
    comment:
      "Maps NordSafe modules to SharePoint list IDs: incidents, riskAssessments, jsa, permitToWork, hazop, minutesOfMeeting, sds",
  },
  msSharepointCustomSite: {
    type: DataTypes.STRING,
    allowNull: true,
    comment: "Custom SharePoint site path (e.g., 'sites/NordSafe'). If null, uses root site.",
  },
});

module.exports = Companies;
