import { Sequelize, DataTypes, Model, Optional } from 'sequelize';
import { UserAttributes } from './User';
import { ClientMasterAttributes } from './client_master';
import { ProjectMasterAttributes } from './project_master';

export interface TaskAttributes {
  id: number;
  client_id: number;
  project_id: number;
  task_type: 'planned' | 'adhoc';
  priority: boolean;
  title: string;
  description: string;
  close_notes: string | null;
  difficulty: 'easy' | 'challenging';
  ai_use: boolean;
  assigned_to?: number;
  status: 'pending' |'assigned' | 'started' | 'escalated' | 'completed' | 'released';
  is_released?: boolean;
  created_by: number;
  is_active?: boolean;
  is_task_assigned?: boolean;
  task_released?: boolean;
  escalated_date?: Date;
  is_adhoc?: boolean;
  never_assigned?:boolean;
  hours?: number;
}

interface TaskCreationAttributes extends Optional<TaskAttributes, 'id'> {}

export class Task
  extends Model<TaskAttributes, TaskCreationAttributes>
  implements TaskAttributes
{
  public static associate(models: any): void {
    Task.belongsTo(models.ProjectMaster, {
      foreignKey: 'project_id',
      as: 'project',
    });
    Task.belongsTo(models.ClientMaster, {
      foreignKey: 'client_id',
      as: 'client',
      targetKey: 'id',
    });
    Task.belongsTo(models.User, {
      foreignKey: 'assigned_to',
      as: 'assignee',
    });
    Task.hasMany(models.Plan, {
      foreignKey: 'task_id',
      as: 'plans'
  });
  Task.belongsTo(models.User, {
    foreignKey: 'created_by',
    as: 'creator'
  });
  Task.hasMany(models.TaskWatcher, {
      foreignKey: 'task_id',
      as: 'task_watchers'
  });
  }
  public id!: number;
  public client_id!: number;
  public project_id!: number;
  public task_type!: 'planned' | 'adhoc';
  public priority!: boolean;
  public title!: string;
  public description!: string;
  public close_notes!: string | null;
  public difficulty!: 'easy' | 'challenging';
  public ai_use!: boolean;
  public assigned_to?: number;
  public status!: 'pending' | 'assigned' | 'started' | 'escalated' | 'completed' | 'released';
  public is_released!: boolean;
  public is_active!: boolean;
  public created_by!: number;
  public is_task_assigned?: boolean;
  public task_released?: boolean;
  public escalated_date?: Date;
  public is_adhoc?: boolean;
  public assignee?: UserAttributes;
  public client?: ClientMasterAttributes;
  public project?: ProjectMasterAttributes;
  public never_assigned?: boolean;
  public hours?: number;

  // public Clients?: ClientMasterAttributes;

  public toJSON(): TaskAttributes & {
    assignee?: UserAttributes;
    client?: ClientMasterAttributes;
    project?: ProjectMasterAttributes;
  } {
    return {
      ...super.toJSON(), // Include parent class serialization
      id: this.id,
      client_id: this.client_id,
      project_id: this.project_id,
      task_type: this.task_type,
      priority: this.priority,
      title: this.title,
      description: this.description,
      close_notes: this.close_notes,
      difficulty: this.difficulty,
      ai_use: this.ai_use,
      assigned_to: this.assigned_to,
      assignee: this.assignee,
      client: this.client, // Add this line
      project: this.project, // Add this line
      status: this.status,
      is_released: this.is_released,
      is_active: this.is_active,
      created_by: this.created_by,
      is_task_assigned: this.is_task_assigned,
      task_released: this.task_released,
      escalated_date: this.escalated_date,
      is_adhoc: this.is_adhoc,
      never_assigned: this.never_assigned,
      hours: this.hours,
    };
  }
}

export default (sequelize: Sequelize, dataTypes: typeof DataTypes) => {
  Task.init(
    {
      id: {
        type: dataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      client_id: {
        type: dataTypes.INTEGER,
        allowNull: false,
        references: {
          model: 'clients',
          key: 'id',
        },
      },
      project_id: {
        type: dataTypes.INTEGER,
        allowNull: false,
        references: {
          model: 'projects',
          key: 'id',
        },
      },
      task_type: {
        type: dataTypes.ENUM('planned', 'adhoc'),
        allowNull: false,
      },
      priority: {
        type: dataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false,
      },
      title: {
        type: dataTypes.STRING,
        allowNull: false,
      },
      description: {
        type: dataTypes.TEXT,
        allowNull: false,
      },
      close_notes: {
        type: dataTypes.TEXT,
        allowNull: true,
      },
      difficulty: {
        type: dataTypes.ENUM('easy', 'challenging'),
        allowNull: true,
      },
      ai_use: {
        type: dataTypes.BOOLEAN,
        allowNull: true,
        defaultValue: null,
      },
      assigned_to: {
        type: dataTypes.INTEGER,
        allowNull: true,
        references: {
          model: 'Users',
          key: 'id',
        },
      },
      status: {
        type: dataTypes.ENUM('pending','assigned', 'started', 'escalated', 'completed', 'released'),
        allowNull: false,
        defaultValue: 'pending',
      },
      is_released: {
        type: dataTypes.BOOLEAN,
        allowNull: true,
        defaultValue: true,
      },
      created_by: {
        type: dataTypes.INTEGER,
        allowNull: true,
        references: {
          model: 'Users',
          key: 'id',
        },
      },
    is_active: {
      type: dataTypes.BOOLEAN,
      allowNull: true,
      defaultValue: true,
    },
    is_task_assigned: {
      type: dataTypes.BOOLEAN,
      allowNull: true,
      defaultValue: false,
    },
    escalated_date: {
      type: dataTypes.DATE,
      allowNull: true,
    },
    is_adhoc: {
      type: dataTypes.BOOLEAN,
      allowNull: true,
      defaultValue: true,
    },
    task_released: {
      type: dataTypes.BOOLEAN,
      allowNull: true,
      defaultValue: false,
    },
    never_assigned: {
      type: dataTypes.BOOLEAN,
      allowNull: true,
      defaultValue: true,
    },
    hours: {
      type: dataTypes.DECIMAL(5, 2), // up to 999.99 hours
      allowNull: true,
    },

    
    },
    {
      sequelize,
      modelName: 'Task',
      tableName: 'tasks',
      timestamps: true,
    }
  );
  // Define associations here


  // The associate method is already defined as a static method in the Task class.
  return Task;
};



