-- أشخاص الصرف + رأس الصرف (Buy) + تفاصيل الصرف (SubBuy)
-- نفّذ على قاعدة uznet_ghs (أو اسم قاعدتك) بعد أخذ نسخة احتياطية.

CREATE TABLE IF NOT EXISTS `Person` (
  `PersonID` int(11) NOT NULL AUTO_INCREMENT,
  `PersonName` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`PersonID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `Buy` (
  `BuyID` int(11) NOT NULL AUTO_INCREMENT,
  `DateNow` date DEFAULT NULL,
  `PersonID` int(11) DEFAULT NULL,
  `DateBuy` date DEFAULT NULL,
  `AmountDi` int(11) DEFAULT NULL,
  `AmountDo` int(11) DEFAULT NULL,
  `Office` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `Activity` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `Month` int(11) DEFAULT NULL,
  `Year` int(11) DEFAULT NULL,
  PRIMARY KEY (`BuyID`),
  KEY `idx_buy_person` (`PersonID`),
  CONSTRAINT `fk_buy_person` FOREIGN KEY (`PersonID`) REFERENCES `Person` (`PersonID`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `SubBuy` (
  `SubBuyID` int(11) NOT NULL AUTO_INCREMENT,
  `BuyID` int(11) NOT NULL,
  `Classify` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  `Type` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  `AmountDi` int(11) DEFAULT NULL,
  `AmountDo` int(11) DEFAULT NULL,
  `Details` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  `Month` int(11) DEFAULT NULL,
  `Year` int(11) DEFAULT NULL,
  PRIMARY KEY (`SubBuyID`),
  KEY `idx_subbuy_buy` (`BuyID`),
  CONSTRAINT `fk_subbuy_buy` FOREIGN KEY (`BuyID`) REFERENCES `Buy` (`BuyID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- تسجيل الشاشات (عدّل المعرفات إن تعارضت مع جدول modules لديك)
INSERT INTO `modules` (`id`, `module_name`, `display_name`) VALUES
(23, 'person_expense_page.dart', 'اشخاص الصرف'),
(24, 'expenses_management_page.dart', 'ادارة الصرفيات')
ON DUPLICATE KEY UPDATE
  `display_name` = VALUES(`display_name`);
