import { create } from 'zustand';

// 租户接口
export interface Tenant {
  id: string;
  name: string;
  code: string;
  description?: string | null;
  status: 'active' | 'inactive' | 'suspended';
  contact_email?: string | null;
  contact_phone?: string | null;
  max_users?: number | null;
  created_at: string;
  updated_at: string;
}

// 用户接口
export interface User {
  id: string;
  username: string;
  email: string;
  full_name?: string | null;
  phone?: string | null;
  role: string;
  status: 'active' | 'inactive' | 'suspended';
  tenant_id: string;
  last_login_at?: string | null;
  created_at: string;
  updated_at: string;
}

// 系统参数接口
export interface SystemParameter {
  id: string;
  key: string;
  value: string;
  description?: string | null;
  category: string;
  data_type: 'string' | 'number' | 'boolean' | 'json';
  is_system: boolean;
  created_at: string;
  updated_at: string;
}

// Config state interface
export interface ConfigState {
  tenants: Tenant[];
  currentTenant: Tenant | null;
  users: User[];
  currentUser: User | null;
  systemParameters: SystemParameter[];
  currentParameter: SystemParameter | null;

  // Actions
  setTenants: (tenants: Tenant[]) => void;
  setCurrentTenant: (tenant: Tenant | null) => void;
  setUsers: (users: User[]) => void;
  setCurrentUser: (user: User | null) => void;
  setSystemParameters: (parameters: SystemParameter[]) => void;
  setCurrentParameter: (parameter: SystemParameter | null) => void;

  // Getters
  getTenants: () => Tenant[];
  getCurrentTenant: () => Tenant | null;
  getUsers: () => User[];
  getCurrentUser: () => User | null;
  getSystemParameters: () => SystemParameter[];
  getCurrentParameter: () => SystemParameter | null;
}

// Create Config store
export const useConfigStore = create<ConfigState>((set, get) => ({
  tenants: [],
  currentTenant: null,
  users: [],
  currentUser: null,
  systemParameters: [],
  currentParameter: null,

  setTenants: (tenants: Tenant[]) => {
    set({ tenants });
  },

  setCurrentTenant: (tenant: Tenant | null) => {
    set({ currentTenant: tenant });
  },

  setUsers: (users: User[]) => {
    set({ users });
  },

  setCurrentUser: (user: User | null) => {
    set({ currentUser: user });
  },

  setSystemParameters: (parameters: SystemParameter[]) => {
    set({ systemParameters: parameters });
  },

  setCurrentParameter: (parameter: SystemParameter | null) => {
    set({ currentParameter: parameter });
  },

  getTenants: () => {
    return get().tenants;
  },

  getCurrentTenant: () => {
    return get().currentTenant;
  },

  getUsers: () => {
    return get().users;
  },

  getCurrentUser: () => {
    return get().currentUser;
  },

  getSystemParameters: () => {
    return get().systemParameters;
  },

  getCurrentParameter: () => {
    return get().currentParameter;
  },
}));

export default useConfigStore;