import { IServerFile } from "@/interfaces/shared/lib/images/IServerFile";

export interface ContactSupport {
  id:           number;
  uuid:         string;

  client_id?:   number | null;
  user_id?:     number | null;

  subject:      string;
  message:      string;
  anwser?:      string | null;

  status:       'open' | 'in_progress' | 'closed';
  priority:     'low' | 'normal' | 'high' | 'urgent';

  attachments?: any[] | null;
  images_path?: IServerFile[];
  meta?:        Record<string, any> | null;

  allow_team_access?: boolean; 

  created_at?:  string;
  updated_at?:  string;
  deleted_at?:  string | null;
}

export type ContactSupportTableRow = Pick<
  ContactSupport,
  'subject'    | 
  'status'     | 
  'priority'   | 
  'created_at' |
  'allow_team_access' 
> & { user_name: string };

export interface ContactSupportClient {
  subject:      string;
  message:      string;
  
  priority?:    'low' | 'normal' | 'high' | 'urgent';
  allow_team_access?: boolean; 

  attachments?:     any[] | null;  // For form create    
  files_path?:      IServerFile[];
}

export interface ContactSupportBackoffice {
  subject:      string;
  message:      string;
  anwser?:      string | null;
  status?:      'open' | 'in_progress' | 'closed';
  priority?:    'low' | 'normal' | 'high' | 'urgent';

  files_path?:        IServerFile[];
  allow_team_access?: boolean; 
}

export type BoContactSupportTableRow = Pick<
  ContactSupport,
  'status'     | 
  'priority'   | 
  'created_at' |
  'allow_team_access' 
> & { 
  client: {
    id: number;
    name: string;
  }
};

export type BoContactSupportAnwser = Pick<
  ContactSupport,
  'anwser' |
  'status'
>;


export const contactSupportClientRules = (t: any) => ({
  subject: [
    (v: any) => !!v || t('settings.support.rules.subject.required'),
    (v: any) => typeof v === 'string' && v.length <= 128 || t('settings.support.rules.subject.max'),
  ],

  message: [
    (v: any) => !!v || t('settings.support.rules.message.required'),
  ],

  attachments: [
    (v: any) => v == null || Array.isArray(v) || t('settings.support.rules.attachments.array'),
  ],

  priority: [
    (v: any) => v == null || ['low', 'normal', 'high', 'urgent'].includes(v) || t('settings.support.rules.priority.in'),
  ],

  allow_team_access: [
    (v: any) => typeof v === 'boolean' || t('settings.support.rules.allow_team_access.boolean'),
  ],

  files_path: [
    (v: any) => v == null || Array.isArray(v) || t('settings.support.rules.files_path.array'),
    (v: any) =>
      v == null || v.every((img: any) => typeof img === 'object' && img !== null) ||
      t('settings.support.rules.files_path.mustBeObjects'),
    (v: any) =>
      v == null || v.every((img: any) => typeof img.url === 'string' && img.url.startsWith('http')) ||
      t('settings.support.rules.files_path.mustHaveValidUrl'),
    (v: any) =>
      v == null || v.every((img: any) => !img.size || img.size <= 3 * 1024 * 1024) ||
      t('settings.support.rules.files_path.maxSize'), // limite 3 Mo (si `size` est présent)
  ],
});

export const contactSupportBackofficeRules = (t: any) => ({
  subject: [
    (v: any) => !!v || t('settings.support.rules.subject.required'),
    (v: any) => typeof v === 'string' && v.length <= 128 || t('settings.support.rules.subject.max'),
  ],

  message: [
    (v: any) => !!v || t('settings.support.rules.message.required'),
  ],

  anwser: [
    (v: any) => v == null || typeof v === 'string' || t('settings.support.rules.anwser.string'),
  ],

  status: [
    (v: any) => v == null || ['open', 'in_progress', 'closed'].includes(v) || t('settings.support.rules.status.in'),
  ],

  priority: [
    (v: any) => v == null || ['low', 'normal', 'high', 'urgent'].includes(v) || t('settings.support.rules.priority.in'),
  ],

  attachments: [
    (v: any) => v == null || Array.isArray(v) || t('settings.support.rules.attachments.array'),
  ],

  meta: [
    (v: any) => v == null || typeof v === 'object' || t('settings.support.rules.meta.object'),
  ],
});

export const contactSupportRules = (t: any) => ({
  subject: [
    (v: any) => !!v || t('settings.support.rules.subject.required'),
    (v: any) => typeof v === 'string' && v.length <= 128 || t('settings.support.rules.subject.max'),
  ],

  message: [
    (v: any) => !!v || t('settings.support.rules.message.required'),
  ],

  anwser: [
    (v: any) => v == null || typeof v === 'string' || t('settings.support.rules.anwser.string'),
  ],

  status: [
    (v: any) => ['open', 'in_progress', 'closed'].includes(v) || t('settings.support.rules.status.in'),
  ],

  priority: [
    (v: any) => ['low', 'normal', 'high', 'urgent'].includes(v) || t('settings.support.rules.priority.in'),
  ],

  attachments: [
    (v: any) => v == null || Array.isArray(v) || t('settings.support.rules.attachments.array'),
  ],

  meta: [
    (v: any) => v == null || typeof v === 'object' || t('settings.support.rules.meta.object'),
  ],
});
