export const DATABASE_ID = process.env.NEXT_PUBLIC_APPWRITE_DATABASE_ID ?? "kovakemlak-db";

export const BUCKETS = {
  propertyImages: "property-images",
  tenantLogos: "tenant-logos",
} as const;

export const TABLES = {
  properties: "properties",
  customers: "customers",
  customerSearches: "customer_searches",
  propertyMatches: "property_matches",
  presentations: "presentations",
  investors: "investors",
  activities: "activities",
  tenantSettings: "tenant_settings",
  inviteLinks: "invite_links",
  deals: "deals",
  passwordResets: "password_resets",
  paymentEvents: "payment_events",
} as const;

export type TableId = (typeof TABLES)[keyof typeof TABLES];

export type SystemRow = {
  $id: string;
  $createdAt: string;
  $updatedAt: string;
  $permissions: string[];
  $databaseId?: string;
  $tableId?: string;
  $sequence?: number;
};

type Row = SystemRow;

export type TenantPlan = "free" | "starter" | "pro" | "enterprise";
export type PlanPeriod = "monthly" | "yearly";
export type TenantRole = "owner" | "admin" | "member";
export type InviteRole = "admin" | "member";

export interface InviteLink extends SystemRow {
  tenantId: string;
  code: string;
  email: string;
  role: InviteRole;
  status: "pending" | "accepted" | "expired" | "cancelled";
  invitedBy: string;
  expiresAt: string;
  acceptedAt?: string;
  acceptedBy?: string;
}

export type PropertyType = "daire" | "villa" | "arsa" | "dukkan" | "ofis" | "depo";
export type ListingType = "satilik" | "kiralik";
export type PropertyStatus = "aktif" | "rezerve" | "pasif" | "satildi" | "kiralandit";
export type CustomerType = "alici" | "kiraci" | "yatirimci";
export type CustomerStage = "ilk_temas" | "aktif_arama" | "teklif" | "sozlesme" | "kapandi";
export type CustomerSource = "referans" | "ilan" | "instagram" | "sahibinden" | "diger";
export type ActivityType = "gorusme" | "teklif" | "ziyaret" | "arama" | "not";
export type DealType = "satis" | "kiralama";
export type DealStatus = "bekleyen" | "tahsil_edildi" | "iptal";

export interface Property extends Row {
  tenantId: string;
  title: string;
  description?: string;
  propertyType: PropertyType;
  listingType: ListingType;
  status: PropertyStatus;
  price: number;
  currency?: string;
  roomCount?: string;
  grossM2?: number;
  netM2?: number;
  floor?: number;
  totalFloors?: number;
  buildingAge?: number;
  city: string;
  district?: string;
  neighborhood?: string;
  address?: string;
  mapLat?: number;
  mapLng?: number;
  featuresJson?: string;
  imageIds?: string;
  createdBy: string;
  assigneeId?: string;
}

export interface Customer extends Row {
  tenantId: string;
  name: string;
  email?: string;
  phone?: string;
  type: CustomerType;
  stage?: CustomerStage;
  source?: CustomerSource;
  nextFollowUpDate?: string;
  assigneeId?: string;
  notes?: string;
  createdBy: string;
}

export interface CustomerSearch extends Row {
  tenantId: string;
  customerId: string;
  listingType?: ListingType;
  propertyTypes?: string;
  roomCounts?: string;
  minPrice?: number;
  maxPrice?: number;
  minM2?: number;
  maxM2?: number;
  cities?: string;
  districts?: string;
  featuresJson?: string;
  isActive?: boolean;
  notes?: string;
  createdBy: string;
  // ağırlıklar: 1=önemsiz … 5=çok önemli
  priceWeight?: number;
  m2Weight?: number;
  locationWeight?: number;
  roomCountWeight?: number;
  propertyTypeWeight?: number;
}

export interface PropertyMatch extends Row {
  tenantId: string;
  propertyId: string;
  customerId: string;
  searchId: string;
  notified?: boolean;
  viewedAt?: string;
  score?: number;
  createdBy: string;
}

export interface Presentation extends Row {
  tenantId: string;
  title: string;
  customerId?: string;
  propertyIds: string;
  shareToken: string;
  expiresAt?: string;
  viewCount?: number;
  notes?: string;
  createdBy: string;
}

export interface Investor extends Row {
  tenantId: string;
  userId?: string;
  name: string;
  email: string;
  phone?: string;
  budget?: number;
  currency?: string;
  notes?: string;
  createdBy: string;
}

export interface Deal extends Row {
  tenantId: string;
  propertyId?: string;
  customerId?: string;
  propertyTitle?: string;
  customerName?: string;
  type: DealType;
  status?: DealStatus;
  salePrice: number;
  commissionRate: number;
  commissionAmount: number;
  officeSharePercent?: number;
  agentSharePercent?: number;
  referralName?: string;
  referralPhone?: string;
  referralPercent?: number;
  agentId: string;
  agentName: string;
  closingDate?: string;
  notes?: string;
  createdBy: string;
}

export interface Activity extends Row {
  tenantId: string;
  customerId?: string;
  propertyId?: string;
  type: ActivityType;
  title: string;
  description?: string;
  dueDate?: string;
  completedAt?: string;
  createdBy: string;
  assigneeId?: string;
}

export interface TenantSettings extends Row {
  tenantId: string;
  officeName?: string;
  logo?: string;
  defaultCurrency?: string;
  phone?: string;
  email?: string;
  address?: string;
  createdBy: string;
  plan?: TenantPlan;
  planPeriod?: PlanPeriod;
  planExpiresAt?: string;
  planProvider?: string;
}

export interface PaymentEvent extends Row {
  tenantId: string;
  plan: Exclude<TenantPlan, "free">;
  period: PlanPeriod;
  amount: number;
  provider: string;
  orderId?: string;
  discountCode?: string;
}

export type PropertyFeature =
  | "balkon"
  | "otopark"
  | "asansor"
  | "havuz"
  | "spor_salonu"
  | "guvenlik"
  | "site_ici"
  | "esyali"
  | "merkezi_isitma"
  | "dogalgaz"
  | "deprem_yalitim"
  | "bahce";

export const ROOM_COUNT_OPTIONS = [
  "Stüdyo",
  "1+0",
  "1+1",
  "2+1",
  "3+1",
  "4+1",
  "4+2",
  "5+1",
  "5+2",
  "6+",
] as const;

export const PROPERTY_TYPE_LABELS: Record<PropertyType, string> = {
  daire: "Daire",
  villa: "Villa",
  arsa: "Arsa",
  dukkan: "Dükkan",
  ofis: "Ofis",
  depo: "Depo",
};

export const LISTING_TYPE_LABELS: Record<ListingType, string> = {
  satilik: "Satılık",
  kiralik: "Kiralık",
};

export const PROPERTY_STATUS_LABELS: Record<PropertyStatus, string> = {
  aktif: "Aktif",
  rezerve: "Rezerve",
  pasif: "Pasif",
  satildi: "Satıldı",
  kiralandit: "Kiralandı",
};

export const CUSTOMER_STAGE_LABELS: Record<CustomerStage, string> = {
  ilk_temas: "İlk Temas",
  aktif_arama: "Aktif Arama",
  teklif: "Teklif",
  sozlesme: "Sözleşme",
  kapandi: "Kapandı",
};

export const CUSTOMER_SOURCE_LABELS: Record<CustomerSource, string> = {
  referans: "Referans",
  ilan: "İlan",
  instagram: "InstagramLogo",
  sahibinden: "Sahibinden",
  diger: "Diğer",
};

export const CUSTOMER_TYPE_LABELS: Record<CustomerType, string> = {
  alici: "Alıcı",
  kiraci: "Kiracı",
  yatirimci: "Yatırımcı",
};

export const DEAL_TYPE_LABELS: Record<DealType, string> = {
  satis: "Satış",
  kiralama: "Kiralama",
};

export const DEAL_STATUS_LABELS: Record<DealStatus, string> = {
  bekleyen: "Bekleyen",
  tahsil_edildi: "Tahsil Edildi",
  iptal: "İptal",
};

export const ACTIVITY_TYPE_LABELS: Record<ActivityType, string> = {
  gorusme: "Görüşme",
  teklif: "Teklif",
  ziyaret: "Ziyaret",
  arama: "Arama",
  not: "Not",
};
